Last active
January 2, 2016 10:39
-
-
Save napratin/8291279 to your computer and use it in GitHub Desktop.
Permutations in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def permutations(s): | |
"""Return all permutations of the characters in a string.""" | |
res = [] | |
for i in xrange(len(s)): | |
c = s[i] | |
rem = s[:i] + s[i+1:] | |
if rem: | |
temp = permutations(rem) | |
res += [c + t for t in temp] | |
else: | |
res += c | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment