Created
November 19, 2015 08:35
-
-
Save jakab922/a377241b9afa98b8191a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from collections import defaultdict | |
from sys import argv | |
def to_dict(word): | |
ret = defaultdict(int) | |
for letter in word: | |
ret[letter] += 1 | |
return ret | |
def anagrams(d, prefix): | |
has_any = False | |
for key in sorted(d.keys()): | |
if d[key] != 0: | |
has_any = True | |
d[key] -= 1 | |
anagrams(d, prefix + key) | |
d[key] += 1 | |
if not has_any: | |
print prefix | |
if __name__ == "__main__": | |
word = argv[1] | |
anagrams(to_dict(word), "") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment