Recursive function to develop permutations
Steve Goldman
steve_g at ix.netcom.com
Wed Oct 20 22:43:31 EDT 2004
Thanks to everyone for lots to think about. I really like the concept of
counting in base "X" as a model for this problem.
But now I have another question. In the elegant recursive generator, below,
the yield expression for the terminating condition is followed by a return
statement. Why is that necessary? I know it is, because I tried taking it
out with unhappy consequences. I thought yield had the same effect as
return, except that the function's state was "frozen" until the next
iteration.
Thanks again.
> Are these "counting" solutions any simpler than the recursive one? I
> don't really think so, for a cleanly-implemented recursion (one without
> globals and a first-time switch...!-) such as:
>
> def permute(Xs, N):
> if N <= 0:
> yield []
> return
> for x in Xs:
> for sub in permute(Xs, N-1):
> yield [x]+sub
>
More information about the Python-list
mailing list