Skip to content

Instantly share code, notes, and snippets.

@rioleo
Created November 15, 2011 19:26
Show Gist options
  • Save rioleo/1368055 to your computer and use it in GitHub Desktop.
Save rioleo/1368055 to your computer and use it in GitHub Desktop.
permutate.py
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