-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathpermutationInversion.py
49 lines (44 loc) · 1.21 KB
/
permutationInversion.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
# http://code.activestate.com/recipes/579051-get-inversion-number-of-a-permutation/
# coding: utf-8
def I(tupPerm):
tmpList=[]
for k in tupPerm:
tmpL=list(tupPerm)
kIndex=tmpL.index(k)
copyTup=[x for x in tmpL]
copyTup2=copyTup[(kIndex+1):]
ctInversionsForKList=[]
for x in copyTup2:
if x<k:
ctInversionsForKList.append(1)
tmpList.append(sum(ctInversionsForKList))
return sum(tmpList)
def p(n,r):
from itertools import permutations
tmp=permutations([x for x in range(1,n+1)],r)
tupPermList=list(tmp)
return tupPermList
def convertStdForm(tupPerm):
tmpS=''
for u in tupPerm:
tmpS+=str(u)
return eval(tmpS)
ans=""
from itertools import permutations
while ans!="N":
print()
n=eval(input("Please enter the int n for which permutations will be taken from: "))
print()
tupPerms=p(n,n)
permsWithLessThan3Inversions=[]
for tup in tupPerms:
if I(tup)<3:
permsWithLessThan3Inversions.append(tup)
stdPermForm=[]
for tup in permsWithLessThan3Inversions:
stdPermForm.append(convertStdForm(tup))
print("The number of permutations of length ",n," from the set ",[x for x in range(1,n+1)]," that have inversion numbers no greater than 2 are given as follows: ")
print()
for x in stdPermForm:
print(x,end=", ")
print()