-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagrams.py
executable file
·43 lines (30 loc) · 971 Bytes
/
anagrams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
"""
anagrams.py - Given a word, returns a list of words of anagrams for it.
"""
from collections import Counter
EN_DICITONARY = 'ciphers/utils/en_dictionary.txt'
def main():
dictionary = load_dictionary(EN_DICITONARY)
user_input = take_user_input('Type in a word: ')
anagram_list = find_anagrams(dictionary, user_input)
if len(anagram_list) == 0:
print('No matches found!')
else:
print('Anagrams found:', *anagram_list, sep='\n')
def find_anagrams(list_of_words, target):
return [w for w in list_of_words if Counter(w.lower()) == Counter(target)]
def take_user_input(prompt_text):
res = input(prompt_text)
while len(res) == 0:
print('You must type at least one word')
res = input(prompt_text)
return res
def load_dictionary(path_to_dict):
dictionary = []
with open(path_to_dict) as f:
for word in f:
dictionary.append(word.strip())
return dictionary
if __name__ == '__main__':
main()