Skip to content

Instantly share code, notes, and snippets.

@beala beala/perm.py
Created May 5, 2012

Embed
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
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, "", [])
@jameslk

This comment has been minimized.

Copy link

commented May 17, 2012

Looks good! Nice solution :)

@jyopari

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.