Skip to content

Instantly share code, notes, and snippets.

@avenet
Last active March 2, 2016 15:02
Show Gist options
  • Save avenet/2b8af5082f33a2f07fbf to your computer and use it in GitHub Desktop.
Save avenet/2b8af5082f33a2f07fbf to your computer and use it in GitHub Desktop.
Prints all of the permutations of a given string
def permutations(str_value):
chars = list(str_value)
used_chars = [False] * len(str_value)
return _permute(chars, used_chars, "")
def _permute(chars, used_chars, current):
if len(current) == len(used_chars):
yield current
else:
for i, used_char in enumerate(used_chars):
if not used_chars[i]:
used_chars[i] = True
for permutation in permute(chars, used_chars, chars[i] + current):
yield permutation
used_chars[i] = False
for p in permutations('gist'):
print p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment