Skip to content

Instantly share code, notes, and snippets.

@thejsj
Created January 9, 2013 19:11
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 thejsj/4495937 to your computer and use it in GitHub Desktop.
Save thejsj/4495937 to your computer and use it in GitHub Desktop.
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:]
@EML-student
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment