Skip to content

Instantly share code, notes, and snippets.

@flyx
Created June 1, 2013 19:49
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 flyx/5691526 to your computer and use it in GitHub Desktop.
Save flyx/5691526 to your computer and use it in GitHub Desktop.
def rec_permutations(prefix, remaining):
if len(remaining) == 1:
yield prefix + remaining
else:
for e in remaining:
new_remaining = remaining[:] # copy
new_remaining.remove(e)
for p in rec_permutations(prefix + [e], new_remaining):
yield p
# sort list before calling
def permutations(dataList):
sortedList = sorted(dataList)
for p in rec_permutations([], sortedList):
yield p
someList = [1, 5, 2, 8, -2]
for p in permutations(someList):
if p[2] == 8: # some condition
result = p
break
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment