Skip to content

Instantly share code, notes, and snippets.

@tuxskar
Last active August 29, 2015 14:19
Show Gist options
  • Save tuxskar/3aafedf24cc100ca0a35 to your computer and use it in GitHub Desktop.
Save tuxskar/3aafedf24cc100ca0a35 to your computer and use it in GitHub Desktop.
Anagram finder
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