Skip to content

Instantly share code, notes, and snippets.

@vivekn
Created March 25, 2012 14:02
Show Gist options
  • Save vivekn/2194844 to your computer and use it in GitHub Desktop.
Save vivekn/2194844 to your computer and use it in GitHub Desktop.
CodeDef - Permutations
def permg(seq):
"""
Returns a generator of permutations for a sequence.
"""
if len(seq) <= 1:
yield seq
else:
for perm in permg(seq[1:]):
for i in range(len(seq)):
yield perm[:i] + seq[0:1] + perm[i:]
result = sorted(permg('0123456789'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment