Created
February 23, 2016 16:56
-
-
Save Tsunamicom/f48b1c093b2466f59042 to your computer and use it in GitHub Desktop.
Python permutations
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 perm(text): | |
if len(text) == 1: | |
return [text] | |
else: | |
sub_perm = perm(text[1:]) | |
first = text[0] | |
permutations = list() | |
for subset in sub_perm: | |
for i in range(len(subset)+1): | |
permutations.append(subset[:i]+ first+subset[i:]) | |
return permutations | |
print(perm('1234')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment