Last active
August 29, 2015 14:19
-
-
Save tuxskar/3aafedf24cc100ca0a35 to your computer and use it in GitHub Desktop.
Anagram finder
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
from collections import OrderedDict | |
N = 2000 | |
WORDS = ["pool", "loco", "cool", "stain", "satin", "loop"] * N + ["pretty", "nice"] | |
def get_anagrams_default_orddict(words=WORDS): | |
normalized = [''.join(sorted(w)) for w in words] | |
d = OrderedDict() | |
for i, n in enumerate(normalized): | |
val = words[i] | |
try: | |
d[n][1] += 1 | |
except KeyError: | |
d[n] = [val, 0] | |
return [v[0] for k, v in d.iteritems() if v[1] >= 1] | |
if __name__ == '__main__': | |
print("Finding the anagrams in words=", WORDS) | |
print(get_anagrams_default_orddict()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment