[SciPy-User] all unique permutations

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


On Thu, Apr 28, 2011 at 8:27 PM,  <josef.pktd at gmail.com> wrote:
> 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]

and

>>> for i in permit(['aa','bbb','c','aa']): print i
...
['aa', 'aa', 'bbb', 'c']
['aa', 'aa', 'c', 'bbb']
['aa', 'bbb', 'aa', 'c']
['aa', 'bbb', 'c', 'aa']
['aa', 'c', 'aa', 'bbb']
['aa', 'c', 'bbb', 'aa']
['bbb', 'aa', 'aa', 'c']
['bbb', 'aa', 'c', 'aa']
['bbb', 'c', 'aa', 'aa']
['c', 'aa', 'aa', 'bbb']
['c', 'aa', 'bbb', 'aa']
['c', 'bbb', 'aa', 'aa']

>
> Josef
>



More information about the SciPy-User mailing list