Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
harshildarji committed Sep 6, 2016
1 parent e1366ff commit ab2161e
Show file tree
Hide file tree
Showing 2 changed files with 235,914 additions and 0 deletions.
28 changes: 28 additions & 0 deletions other/anagrams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import collections, pprint, time, os

start_time = time.time()
print('creating word list...')
path = os.path.split(os.path.realpath(__file__))
word_list = sorted(list(set([word.strip().lower() for word in open(path[0] + '/words')])))

def signature(word):
return ''.join(sorted(word))

word_bysig = collections.defaultdict(list)
for word in word_list:
word_bysig[signature(word)].append(word)

def anagram(myword):
return word_bysig[signature(myword)]

print('finding anagrams...')
all_anagrams = {word: anagram(word)
for word in word_list if len(anagram(word)) > 1}

print('writing anagrams to file...')
with open('anagrams.txt', 'w') as file:
file.write('all_anagrams = ')
file.write(pprint.pformat(all_anagrams))

total_time = round(time.time() - start_time, 2)
print('Done [', total_time, 'seconds ]')
Loading

0 comments on commit ab2161e

Please sign in to comment.