Created
June 4, 2014 04:32
-
-
Save Bolt64/11ccaa105fce2eb1b15b to your computer and use it in GitHub Desktop.
A script to recursively generate permutations in lexicographic order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
def generate_permutations(sequence): | |
if len(sequence)==1: | |
yield sequence | |
elif len(sequence)>1: | |
for index, item in enumerate(sequence): | |
for perm in generate_permutations(sequence[:index]+sequence[index+1:]): | |
yield [item]+perm | |
if __name__=="__main__": | |
from sys import argv | |
total=int(argv[-1]) | |
seq=list(range(total)) | |
for i in generate_permutations(seq): | |
print(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment