mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
0c5f1c0130
* Increased Readability Of Variables * Update anagrams.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update anagrams.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
import collections
|
|
import pprint
|
|
from pathlib import Path
|
|
|
|
|
|
def signature(word: str) -> str:
|
|
"""Return a word sorted
|
|
>>> signature("test")
|
|
'estt'
|
|
>>> signature("this is a test")
|
|
' aehiisssttt'
|
|
>>> signature("finaltest")
|
|
'aefilnstt'
|
|
"""
|
|
return "".join(sorted(word))
|
|
|
|
|
|
def anagram(my_word: str) -> list[str]:
|
|
"""Return every anagram of the given word
|
|
>>> anagram('test')
|
|
['sett', 'stet', 'test']
|
|
>>> anagram('this is a test')
|
|
[]
|
|
>>> anagram('final')
|
|
['final']
|
|
"""
|
|
return word_by_signature[signature(my_word)]
|
|
|
|
|
|
data: str = Path(__file__).parent.joinpath("words.txt").read_text(encoding="utf-8")
|
|
word_list = sorted({word.strip().lower() for word in data.splitlines()})
|
|
|
|
word_by_signature = collections.defaultdict(list)
|
|
for word in word_list:
|
|
word_by_signature[signature(word)].append(word)
|
|
|
|
if __name__ == "__main__":
|
|
all_anagrams = {word: anagram(word) for word in word_list if len(anagram(word)) > 1}
|
|
|
|
with open("anagrams.txt", "w") as file:
|
|
file.write("all_anagrams = \n ")
|
|
file.write(pprint.pformat(all_anagrams))
|