Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://oeis.org/A280319
- # listing the permutations generated by Steinhaus–Johnson–Trotter algorithm as their reverse colexicographic index numbers
- # s_permutations comes from https://rosettacode.org/wiki/Permutations_by_swapping#Python:_recursive
- from itertools import permutations
- from bidict import bidict
- from math import factorial
- m = 7
- sjt_numbers = []
- for n in range(1, m+1):
- # generate n-element permutations in reverse colexicographic order
- perms_iter = permutations(list(range(n)))
- perms_revcolex_bidict = bidict()
- for i, perm in enumerate(perms_iter):
- perms_revcolex_bidict[factorial(n)-1-i] = tuple(perm[::-1]) # create reverse colexicographic order
- # generate n-element permutations in Steinhaus–Johnson–Trotter order
- def s_permutations(seq):
- def s_perm(seq):
- if not seq:
- return [[]]
- else:
- new_items = []
- for i, item in enumerate(s_perm(seq[:-1])):
- if i % 2:
- # step up
- new_items += [item[:i] + seq[-1:] + item[i:]
- for i in range(len(item) + 1)]
- else:
- # step down
- new_items += [item[:i] + seq[-1:] + item[i:]
- for i in range(len(item), -1, -1)]
- return new_items
- return [tuple(item) for item in s_perm(seq)]
- perms_sjt = s_permutations(list(range(n)))
- # assign rev colex index numbers to permutations
- for i in range(factorial(n)):
- perm = perms_sjt[i]
- sjt_number = perms_revcolex_bidict.inv[perm]
- sjt_numbers.append(sjt_number)
- print(sjt_numbers)
- b_file_text = ''
- for i, e in enumerate(sjt_numbers):
- b_file_text += '%s %s\n' % (i, e)
- b_file = open('b_file', 'w')
- b_file.write(b_file_text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement