Last active
August 29, 2015 14:22
-
-
Save iAnatoly/0d59fd931cdc929a6708 to your computer and use it in GitHub Desktop.
Permutations in natural vs. lexigraphical 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
def lex_permutations(array): | |
if len(array) < 2: | |
yield array | |
else: | |
for i in range(0, len(array)): | |
for shorter_permutation in lex_permutations(array[:i] + array[i+1:]): | |
yield array[i:i+1] + shorter_permutation |
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
def permutations(array): | |
if len(array) < 2: | |
yield array | |
else: | |
for shorter_permutation in permutations(array[1:]): | |
for i in range(len(array)): | |
yield shorter_permutation[:i] + array[0:1] + shorter_permutation[i:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment