Skip to content

Commit ab2161e

Browse files
committed
initial
1 parent e1366ff commit ab2161e

File tree

2 files changed

+235914
-0
lines changed

2 files changed

+235914
-0
lines changed

other/anagrams.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import collections, pprint, time, os
2+
3+
start_time = time.time()
4+
print('creating word list...')
5+
path = os.path.split(os.path.realpath(__file__))
6+
word_list = sorted(list(set([word.strip().lower() for word in open(path[0] + '/words')])))
7+
8+
def signature(word):
9+
return ''.join(sorted(word))
10+
11+
word_bysig = collections.defaultdict(list)
12+
for word in word_list:
13+
word_bysig[signature(word)].append(word)
14+
15+
def anagram(myword):
16+
return word_bysig[signature(myword)]
17+
18+
print('finding anagrams...')
19+
all_anagrams = {word: anagram(word)
20+
for word in word_list if len(anagram(word)) > 1}
21+
22+
print('writing anagrams to file...')
23+
with open('anagrams.txt', 'w') as file:
24+
file.write('all_anagrams = ')
25+
file.write(pprint.pformat(all_anagrams))
26+
27+
total_time = round(time.time() - start_time, 2)
28+
print('Done [', total_time, 'seconds ]')

0 commit comments

Comments
 (0)