Last active
March 2, 2016 15:02
-
-
Save avenet/2b8af5082f33a2f07fbf to your computer and use it in GitHub Desktop.
Prints all of the permutations of a given string
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(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