mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
edited strings/anagram.py (#5770)
* rewrote anagrams.py, added doctests * corrected mistakes * add anagrams.txt * Update anagrams.py * Update strings/anagrams.py Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
a8aeabdf18
commit
4c9949f636
|
@ -1,35 +1,44 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
import os
|
|
||||||
import pprint
|
import pprint
|
||||||
import time
|
from pathlib import Path
|
||||||
|
|
||||||
start_time = time.time()
|
|
||||||
print("creating word list...")
|
|
||||||
path = os.path.split(os.path.realpath(__file__))
|
|
||||||
with open(path[0] + "/words.txt") as f:
|
|
||||||
word_list = sorted(list({word.strip().lower() for word in f}))
|
|
||||||
|
|
||||||
|
|
||||||
def signature(word):
|
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))
|
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_bysig[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_bysig = collections.defaultdict(list)
|
word_bysig = collections.defaultdict(list)
|
||||||
for word in word_list:
|
for word in word_list:
|
||||||
word_bysig[signature(word)].append(word)
|
word_bysig[signature(word)].append(word)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
all_anagrams = {word: anagram(word) for word in word_list if len(anagram(word)) > 1}
|
||||||
|
|
||||||
def anagram(my_word):
|
with open("anagrams.txt", "w") as file:
|
||||||
return word_bysig[signature(my_word)]
|
file.write("all_anagrams = \n ")
|
||||||
|
|
||||||
|
|
||||||
print("finding anagrams...")
|
|
||||||
all_anagrams = {word: anagram(word) for word in word_list if len(anagram(word)) > 1}
|
|
||||||
|
|
||||||
print("writing anagrams to file...")
|
|
||||||
with open("anagrams.txt", "w") as file:
|
|
||||||
file.write("all_anagrams = ")
|
|
||||||
file.write(pprint.pformat(all_anagrams))
|
file.write(pprint.pformat(all_anagrams))
|
||||||
|
|
||||||
total_time = round(time.time() - start_time, 2)
|
|
||||||
print(("Done [", total_time, "seconds ]"))
|
|
||||||
|
|
33957
strings/anagrams.txt
Normal file
33957
strings/anagrams.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user