Skip to content

Commit 479e7c1

Browse files
committed
keep it simple: anagram checker
1 parent 1f24f63 commit 479e7c1

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

anagrams.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
anagrams.py - Given a word, returns a list of words of anagrams for it.
55
"""
66

7+
from collections import Counter
8+
79
EN_DICITONARY = 'ciphers/utils/en_dictionary.txt'
810

911
def main():
@@ -17,17 +19,14 @@ def main():
1719
print('Anagrams found:', *anagram_list, sep='\n')
1820

1921

20-
def find_anagrams(list_of_words, word):
21-
anagrams = []
22-
target = sorted(word.lower())
23-
[anagrams.append(w) for w in list_of_words if sorted(w.lower()) == target]
24-
return anagrams
22+
def find_anagrams(list_of_words, target):
23+
return [w for w in list_of_words if Counter(w.lower()) == Counter(target)]
2524

2625

2726
def take_user_input(prompt_text):
2827
res = input(prompt_text)
29-
while len(res.strip().split()) != 1:
30-
print('You must type in a single word')
28+
while len(res) == 0:
29+
print('You must type at least one word')
3130
res = input(prompt_text)
3231
return res
3332

0 commit comments

Comments
 (0)