Last active
August 29, 2015 14:24
-
-
Save debetimi/79b4111e46078e1ff6cd to your computer and use it in GitHub Desktop.
Permutation
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 memoize(func): | |
cache = {} | |
def memo(string): | |
if not string in cache: | |
cache[string] = func(string) | |
return cache[string] | |
return memo | |
@memoize | |
def permutations(string): | |
if not string: | |
return [''] | |
results = [] | |
chars_used = [] | |
for i, ch in enumerate(string): | |
if ch in chars_used: | |
continue | |
chars_used.append(ch) | |
for substr in permutations(string[:i]+string[i+1:]): | |
results.append(ch + substr) | |
return results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment