File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33import collections
44import pprint
55from pathlib import Path
6- from typing import List
76
87
98def signature (word : str ) -> str :
109 """
11- Return a frequency-based signature for a word .
10+ Return a word's frequency-based signature.
1211
1312 >>> signature("test")
1413 'e1s1t2'
@@ -21,7 +20,7 @@ def signature(word: str) -> str:
2120 return "" .join (f"{ ch } { freq [ch ]} " for ch in sorted (freq ))
2221
2322
24- def anagram (my_word : str ) -> List [str ]:
23+ def anagram (my_word : str ) -> list [str ]:
2524 """
2625 Return every anagram of the given word from the dictionary.
2726
@@ -35,11 +34,9 @@ def anagram(my_word: str) -> List[str]:
3534 return word_by_signature .get (signature (my_word ), [])
3635
3736
38- # Load word list
3937data : str = Path (__file__ ).parent .joinpath ("words.txt" ).read_text (encoding = "utf-8" )
4038word_list = sorted ({word .strip ().lower () for word in data .splitlines ()})
4139
42- # Map signatures to word list
4340word_by_signature = collections .defaultdict (list )
4441for word in word_list :
4542 word_by_signature [signature (word )].append (word )
You can’t perform that action at this time.
0 commit comments