from itertools import permutations 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) count = 1 with open('A268532b.txt','w') as f: for n in range(1,10): tupPerms=p(n,n) final = [] for tup in tupPerms: final.append([I(tup),tup]) final.sort(key=lambda x: x[0]) for x in final: f.write(str(count)+" "+''.join(str(y) for y in x[1])+"\n") count += 1 #Thanks to Samuel James Erickson for the python recipe #See http://code.activestate.com/recipes/users/4187478/ #for more of his work. #David Consiglio, Jr. 02 Feb 2016