-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathkth_lexicographic_permutation.py
More file actions
49 lines (38 loc) · 1.3 KB
/
kth_lexicographic_permutation.py
File metadata and controls
49 lines (38 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def kth_permutation(k, n):
"""
Finds k'th lexicographic permutation (in increasing order) of
0,1,2,...,n-1 in O(n^2) time.
Examples:
First permutation is always 0,1,2,...,n-1
>>> kth_permutation(0,5)
[0, 1, 2, 3, 4]
The order of permutation of 0,1,2,3 is [0,1,2,3], [0,1,3,2], [0,2,1,3],
[0,2,3,1], [0,3,1,2], [0,3,2,1], [1,0,2,3], [1,0,3,2], [1,2,0,3],
[1,2,3,0], [1,3,0,2]
>>> kth_permutation(10,4)
[1, 3, 0, 2]
"""
# Factorials from 1! to (n-1)!
if not isinstance(k, int) or not isinstance(n, int):
raise TypeError("k and n must be integers")
if n < 1:
raise ValueError("n must be a positive integer")
factorials = [1]
for i in range(2, n):
factorials.append(factorials[-1] * i)
max_k = factorials[-1] * n # equals n!
if not (0 <= k < max_k):
raise ValueError("k out of bounds")
permutation = []
elements = list(range(n))
# Find permutation
while factorials:
factorial = factorials.pop()
index, k = divmod(k, factorial)
permutation.append(elements[index])
elements.pop(index)
permutation.append(elements[0])
return permutation
if __name__ == "__main__":
import doctest
doctest.testmod()