Skip to content

Commit eb6ec62

Browse files
authored
Refactor anagram function return type to list[str]
1 parent 13e7694 commit eb6ec62

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

strings/anagrams.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
import collections
44
import pprint
55
from pathlib import Path
6-
from typing import List
76

87

98
def 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
3937
data: str = Path(__file__).parent.joinpath("words.txt").read_text(encoding="utf-8")
4038
word_list = sorted({word.strip().lower() for word in data.splitlines()})
4139

42-
# Map signatures to word list
4340
word_by_signature = collections.defaultdict(list)
4441
for word in word_list:
4542
word_by_signature[signature(word)].append(word)

0 commit comments

Comments
 (0)