A permutation on permutations

phil hunt philh at comuno.freeserve.co.uk
Sat Nov 24 21:58:17 EST 2001


On Sat, 24 Nov 2001 19:22:05 -0500, Arthur Siegel <ajs at ix.netcom.com> wrote:
>Was able to find nice stuff on the python-list archives 
>as to list permutations, list reversal, and removing 
>duplicates from lists.
>
>My problem is to combine them.
>
>I am feeding control points to a curve function, and
>want all permutations that result in unique curves.  
>For this purpose a sequence is duplicative 
>of its reverse, i.e. [1,2,3,4] == [4,3,2,1], etc.

Can you have the same number appearing twice, e.g. [1,2,3,3] ?

>So am a looking for a function to return all
>permutations, excluding duplicates as defined above.

Consider the following source:

def perm(source, done=0, current=[]):
   if done == len(source): 
      if current[0] < current[-1]: #removes reversals
         print current
   else:
      for i in source:
         if i not in current:
            perm(source, done+1, current+[i])

d = [1,2,3,4]
perm(d)

When run produces:

[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
[1, 4, 2, 3]
[1, 4, 3, 2]
[2, 1, 3, 4]
[2, 1, 4, 3]
[2, 3, 1, 4]
[2, 4, 1, 3]
[3, 1, 2, 4]
[3, 2, 1, 4]


-- 
*** Philip Hunt *** philh at comuno.freeserve.co.uk ***




More information about the Python-list mailing list