Partition Problem

Tom Good Tom_Good1 at excite.com
Mon Jul 23 12:57:09 EDT 2001


Duncan Booth <duncan at NOSPAMrcp.co.uk> wrote in message news:<Xns90E765BFABFB7duncanrcpcouk at 127.0.0.1>...

[snip]

> Followed by the 'how to iterate over all permutations of a sequence'. BTW, 
> the 'return' statement isn't actually needed, but I think it makes the code 
> a bit clearer.
> --- begin ---
> # Permutations using generators.
> from __future__ import generators
> 
> def permute(seq):
>     if len(seq)==1:
>         yield seq
>         return
> 
>     for i in range(len(seq)):
>         for perm in permute(seq[:i] + seq[i+1:]):
>             yield seq[i:i+1] + perm
> 
> def test(str):
>     for t in permute(str):
>         print t
> 
> test('bar')
> test('flip')
> test([1, 2, 3])
> --- end ---

Bravo!  That's a good one.  When you say you prefer having the
"return" in there, is your purpose to emphasize that execution does
not "fall through" the first "yield"?


Tom



More information about the Python-list mailing list