Generation of Combinatorial Objects · Object Indices (Optional)

Lesson 3

Nikolai Chukhin · Alexander S. Kulikov

Let's learn how to find the index of a given permutation of the numbers \(\{0,1,\dotsc,n-1\}\), assuming that permutations are indexed from zero in lexicographic order:

from itertools import permutations

for i, p in enumerate(permutations(range(3))):
    print(i, p)

0 (0, 1, 2)
1 (0, 2, 1)
2 (1, 0, 2)
3 (1, 2, 0)
4 (2, 0, 1)
5 (2, 1, 0)