[SciPy-User] all unique permutations

josef.pktd at gmail.com josef.pktd at gmail.com
Thu Apr 28 20:27:22 EDT 2011


is there a copyright on basic algorithms ?

from
http://www.cut-the-knot.org/do_you_know/AllPerm.shtml
I translated
2.Lexicographic order and finding the next permutation

into python (because I couldn't get the matlab translation to work)

looks useful for some permutation test

some examples

>>> pprint(list(permit([1,1,1,1,0])))
[[0, 1, 1, 1, 1],
 [1, 0, 1, 1, 1],
 [1, 1, 0, 1, 1],
 [1, 1, 1, 0, 1],
 [1, 1, 1, 1, 0]]

>>> for i in permit([1,0,1,0]): print i
...
[0, 0, 1, 1]
[0, 1, 0, 1]
[0, 1, 1, 0]
[1, 0, 0, 1]
[1, 0, 1, 0]
[1, 1, 0, 0]

>>> for i in permit([1,0,2,2]): print i
...
[0, 1, 2, 2]
[0, 2, 1, 2]
[0, 2, 2, 1]
[1, 0, 2, 2]
[1, 2, 0, 2]
[1, 2, 2, 0]
[2, 0, 1, 2]
[2, 0, 2, 1]
[2, 1, 0, 2]
[2, 1, 2, 0]
[2, 2, 0, 1]
[2, 2, 1, 0]

>>> for i in permit([1,3]): print i
...
[1, 3]
[3, 1]

>>> for i in permit([1,2.3]): print i
...
[1, 2.2999999999999998]
[2.2999999999999998, 1]

>>> for i in permit([1,2.5,3.5]): print i
...
[1, 2.5, 3.5]
[1, 3.5, 2.5]
[2.5, 1, 3.5]
[2.5, 3.5, 1]
[3.5, 1, 2.5]
[3.5, 2.5, 1]

Josef



More information about the SciPy-User mailing list