Re: Permutation Generator

2005-08-15 Thread Gerard Flanagan
Talin wrote: > I'm sure I am not the first person to do this, but I wanted to share > this: a generator which returns all permutations of a list: > > def permute( lst ): > if len( lst ) == 1: > yield lst > else: > head = lst[:1] > for x in permute( lst[1:] ): >

Re: Permutation Generator

2005-08-15 Thread Tom Anderson
On Mon, 15 Aug 2005, Matt Hammond wrote: > Just satisfied my curiosity wrt this problem, so I might as well share :-) > def permute(list): How about: def permutation(l, i): "Makes the ith permutation of the sequence l." # leave out the reverses if you don't care about the or

Re: Permutation Generator

2005-08-15 Thread Matt Hammond
Just satisfied my curiosity wrt this problem, so I might as well share :-) >>> def permute(list): ... if len(list) <= 1: ... yield list ... else: ... for i in xrange(0,len(list)): ... for tail in permute( list[:i] + list[i+1:] ): ... yield [ list

Re: Permutation Generator

2005-08-14 Thread David Isaac
"Casey Hawthorne" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > It's hard to make "complete" permutation generators, Knuth has a whole > fascicle on it - "The Art of Computer Programming - Volume 4 Fascicle > 2 - Generating All Tuples and Permutations" - 2005 Can you elaborate a

Re: Permutation Generator

2005-08-14 Thread Casey Hawthorne
It's hard to make "complete" permutation generators, Knuth has a whole fascicle on it - "The Art of Computer Programming - Volume 4 Fascicle 2 - Generating All Tuples and Permutations" - 2005 -- Regards, Casey -- http://mail.python.org/mailman/listinfo/python-list

Re: Permutation Generator

2005-08-14 Thread Jack Diederich
On Fri, Aug 12, 2005 at 03:48:38PM -0400, Michael J. Fromberger wrote: > In article <[EMAIL PROTECTED]>, > Talin <[EMAIL PROTECTED]> wrote: > > > I'm sure I am not the first person to do this, but I wanted to share > > this: a generator which returns all permutations of a list: > > You're right

Re: Permutation Generator

2005-08-13 Thread Jim Washington
On Fri, 12 Aug 2005 12:39:08 -0700, Talin wrote: > I'm sure I am not the first person to do this, but I wanted to share > this: a generator which returns all permutations of a list: > > def permute( lst ): > if len( lst ) == 1: > yield lst > else: > head = lst[:1] >

Re: Permutation Generator

2005-08-13 Thread David Isaac
"Talin" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I wanted to share > this: a generator which returns all permutations of a list: Try this instead: def permuteg(lst): return ([lst[i]]+x for i in range(len(lst)) for x in permute(lst[:i]+lst[i+1:])) \ or [[]

Re: Permutation Generator

2005-08-12 Thread Paul Rubin
Talin <[EMAIL PROTECTED]> writes: > I'm sure I am not the first person to do this, but I wanted to share > this: a generator which returns all permutations of a list: > > def permute( lst ): > if len( lst ) == 1: > yield lst > else: > head = lst[:1] > for x in permu

Re: Permutation Generator

2005-08-12 Thread Michael J. Fromberger
In article <[EMAIL PROTECTED]>, Talin <[EMAIL PROTECTED]> wrote: > I'm sure I am not the first person to do this, but I wanted to share > this: a generator which returns all permutations of a list: > > def permute( lst ): > if len( lst ) == 1: > yield lst > else: > head