Created
January 9, 2013 19:11
-
-
Save thejsj/4495937 to your computer and use it in GitHub Desktop.
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 all_perms(elements): | |
if len(elements) <=1: | |
yield elements | |
else: | |
for perm in all_perms(elements[1:]): | |
for i in range(len(elements)): | |
#nb elements[0:1] works in both string and list contexts | |
yield perm[:i] + elements[0:1] + perm[i:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So I'm pretty new to recursive programming. How does this generate all possible permutations. The line "for perm in...." should it not find all characters in elements?