2021-11-08 18:58:15 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2020-05-22 08:10:11 +02:00
|
|
|
import collections
|
|
|
|
|
import pprint
|
2021-11-08 18:58:15 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def signature(word: str) -> str:
|
2025-08-24 18:03:18 +05:30
|
|
|
"""
|
|
|
|
|
Return a word's frequency-based signature.
|
|
|
|
|
|
2021-11-08 18:58:15 +01:00
|
|
|
>>> signature("test")
|
2025-08-24 18:03:18 +05:30
|
|
|
'e1s1t2'
|
2021-11-08 18:58:15 +01:00
|
|
|
>>> signature("this is a test")
|
2025-08-24 18:03:18 +05:30
|
|
|
' 3a1e1h1i2s3t3'
|
2021-11-08 18:58:15 +01:00
|
|
|
>>> signature("finaltest")
|
2025-08-24 18:03:18 +05:30
|
|
|
'a1e1f1i1l1n1s1t2'
|
2021-11-08 18:58:15 +01:00
|
|
|
"""
|
2025-08-24 18:03:18 +05:30
|
|
|
frequencies = collections.Counter(word)
|
|
|
|
|
return "".join(
|
|
|
|
|
f"{char}{frequency}" for char, frequency in sorted(frequencies.items())
|
|
|
|
|
)
|
2016-09-06 18:04:53 +05:30
|
|
|
|
|
|
|
|
|
2021-11-08 18:58:15 +01:00
|
|
|
def anagram(my_word: str) -> list[str]:
|
2025-08-24 18:03:18 +05:30
|
|
|
"""
|
|
|
|
|
Return every anagram of the given word from the dictionary.
|
|
|
|
|
|
2021-11-08 18:58:15 +01:00
|
|
|
>>> anagram('test')
|
|
|
|
|
['sett', 'stet', 'test']
|
|
|
|
|
>>> anagram('this is a test')
|
|
|
|
|
[]
|
|
|
|
|
>>> anagram('final')
|
|
|
|
|
['final']
|
|
|
|
|
"""
|
2022-10-30 03:11:17 -04:00
|
|
|
return word_by_signature[signature(my_word)]
|
2019-10-05 01:14:13 -04:00
|
|
|
|
|
|
|
|
|
2021-11-08 18:58:15 +01:00
|
|
|
data: str = Path(__file__).parent.joinpath("words.txt").read_text(encoding="utf-8")
|
|
|
|
|
word_list = sorted({word.strip().lower() for word in data.splitlines()})
|
2016-09-06 18:04:53 +05:30
|
|
|
|
2022-10-30 03:11:17 -04:00
|
|
|
word_by_signature = collections.defaultdict(list)
|
2016-09-06 18:04:53 +05:30
|
|
|
for word in word_list:
|
2022-10-30 03:11:17 -04:00
|
|
|
word_by_signature[signature(word)].append(word)
|
2016-09-06 18:04:53 +05:30
|
|
|
|
2021-11-08 18:58:15 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
all_anagrams = {word: anagram(word) for word in word_list if len(anagram(word)) > 1}
|
2019-10-05 01:14:13 -04:00
|
|
|
|
2021-11-08 18:58:15 +01:00
|
|
|
with open("anagrams.txt", "w") as file:
|
2025-08-24 18:03:18 +05:30
|
|
|
file.write("all_anagrams = \n")
|
2021-11-08 18:58:15 +01:00
|
|
|
file.write(pprint.pformat(all_anagrams))
|