Skip to content

Instantly share code, notes, and snippets.

@beala
Created May 5, 2012 19:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beala/2604992 to your computer and use it in GitHub Desktop.
Save beala/2604992 to your computer and use it in GitHub Desktop.
Permutations interview question.
# Print each permutation.
def perm(l, n, str_a):
if len(str_a) == n:
print str_a
else:
for c in l:
perm(l, n, str_a+c)
# Return a list of permutations.
def perm2(l, n, str_a, perm_a):
if len(str_a) == n:
return [str_a] + perm_a
else:
new_perm_a = perm_a
for c in l:
new_perm_a = perm2(l, n, str_a+c, new_perm_a)
return new_perm_a
perm(['a','b','c'], 3, "")
print perm2(['a','b','c'], 3, "", [])
@jyopari
Copy link

jyopari commented Mar 28, 2015

Good Job, keep making tutorials you explain everything so well.

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