Created
November 15, 2011 19:26
-
-
Save rioleo/1368055 to your computer and use it in GitHub Desktop.
permutate.py
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 permutate(word): | |
returnList = [] | |
if len(word) == 1: | |
returnList.append(word) | |
for pos in range(len(word)): | |
# Recursively find permutations of the word | |
# that does not contain the letter in position | |
permuteList = permutate(word[0:pos] + word[pos+1:len(word)]) | |
# For each of those permutations, add | |
# the missing letter in the right position | |
for item in permuteList: | |
returnList.append(word[pos] + item) | |
# Make this set unique | |
return list(set(returnList)) | |
permutate("hello") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment