Unyeilding a permutation generator

sillyhat at yahoo.com sillyhat at yahoo.com
Tue Nov 4 02:59:17 EST 2008


On 2 Nov, 22:03, Carsten Haese <carsten.ha... at gmail.com> wrote:
> silly... at yahoo.com wrote:
> > Anyway what I want to do is experiment with code similar to this (i.e.
> > same algorithm and keep the recursion) in other languages,
> > particularly vbscript and wondered what it would look like if it was
> > rewritten to NOT use the yield statement
>
> An obvious (though memory-inefficient) replacement is to accumulate a
> list and return the list. Initialize a result variable to an empty list,
> and instead of yielding elements, append them to the result variable.
> Then return the result variable at the end of the function.
>
> HTH,
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

That did it and I can still understand it!

def allperm(str):
  if len(str) <= 1:
     return str
  else:
     biglist = []
     for perm in allperm(str[1:]):
        for i in xrange(len(str)): #minor change here
           biglist.append(perm[:i] + str[0:1] + perm[i:])

  return biglist

for x in allperm("ABCD"):
   print x



More information about the Python-list mailing list