Skip to content

Instantly share code, notes, and snippets.

@iAnatoly
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iAnatoly/0d59fd931cdc929a6708 to your computer and use it in GitHub Desktop.
Save iAnatoly/0d59fd931cdc929a6708 to your computer and use it in GitHub Desktop.
Permutations in natural vs. lexigraphical order
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
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