File tree 1 file changed +11
-6
lines changed
1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change 3
3
import collections
4
4
import pprint
5
5
from pathlib import Path
6
+ from typing import List
6
7
7
8
8
9
def signature (word : str ) -> str :
9
- """Return a word sorted
10
+ """
11
+ Return a word sorted by its letters.
12
+
10
13
>>> signature("test")
11
14
'estt'
12
15
>>> signature("this is a test")
@@ -17,16 +20,18 @@ def signature(word: str) -> str:
17
20
return "" .join (sorted (word ))
18
21
19
22
20
- def anagram (my_word : str ) -> list [str ]:
21
- """Return every anagram of the given word
23
+ def anagram (my_word : str ) -> List [str ]:
24
+ """
25
+ Return every anagram of the given word.
26
+
22
27
>>> anagram('test')
23
28
['sett', 'stet', 'test']
24
29
>>> anagram('this is a test')
25
30
[]
26
31
>>> anagram('final')
27
32
['final']
28
33
"""
29
- return word_by_signature [ signature (my_word )]
34
+ return word_by_signature . get ( signature (my_word ), [])
30
35
31
36
32
37
data : str = Path (__file__ ).parent .joinpath ("words.txt" ).read_text (encoding = "utf-8" )
@@ -39,6 +44,6 @@ def anagram(my_word: str) -> list[str]:
39
44
if __name__ == "__main__" :
40
45
all_anagrams = {word : anagram (word ) for word in word_list if len (anagram (word )) > 1 }
41
46
42
- with open ("anagrams.txt" , "w" ) as file :
43
- file .write ("all_anagrams = \n " )
47
+ with open ("anagrams.txt" , "w" , encoding = "utf-8" ) as file :
48
+ file .write ("all_anagrams = \n " )
44
49
file .write (pprint .pformat (all_anagrams ))
You can’t perform that action at this time.
0 commit comments