Just in case someone is interested in a solution that can find the "i-th" permutation when you look at the "r-length-permutations" (as represented by the r
argument of itertools.permutations
):
from math import factorial
def ith_permutation(i, seq, r=None):
li = list(seq)
length = len(li)
if r is None:
r = length
res = []
current_factorial = factorial(length) // factorial(length - r)
if current_factorial <= i:
raise ValueError('out of range')
for x in range(length, length-r, -1):
current_factorial //= x
div, mod = divmod(i, current_factorial)
i = mod
res.append(li[div])
del(li[div])
return res
For example:
>>> ith_permutationutation(10, [0, 1, 2, 3, 4], 2)
[2, 3]
>>> # correctness check:
>>> from itertools import permutations
>>> list(permutations([0, 1, 2, 3, 4], 2))[10]
(2, 3)
Including a more complete test:
s = range(8)
for n in range(len(s)):
for idx, item in enumerate(permutations(s, n)):
assert list(item) == ith_permutation(idx, s, n)
Some parts of Karoly Horvath's answer were used here.