-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutation2.py
49 lines (36 loc) · 916 Bytes
/
permutation2.py
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
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
#Author Παναγιώτης Πράττης/Panagiotis Prattis
'''
A program that accepts two integers n and k as inputs and prints the permutation of [n]
which is at position k in the lexicographic order of all its permutations of [n].
'''
def factorial(number):
if (number == 0):
f = 1
return f
else:
f = 1
while (number != 0):
f = f*number
number = number - 1
return f
#Main
m=[]
m1=[]
m2=[]
n = int(input ("Give number for permutation length: "))
print(n)
for i in range(n):
m.append(i+1)
m1.append(i+1)
k = int(input ("Give permutation's rank: "))
print(k)
while m:
f = factorial(len(m) - 1)
idx, k = divmod(k, f)
m2.append(m.pop(idx))
assert k == 0
print("The permutation is this:")
print(m1)
print(m2)