Skip to content

Instantly share code, notes, and snippets.

Created May 5, 2012
What would you like to do?
Permutations interview question.
# Print each permutation.
def perm(l, n, str_a):
if len(str_a) == n:
print str_a
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
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, "", [])
Copy link

jameslk commented May 17, 2012

Looks good! Nice solution :)

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