mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
7f04e5cd34
* spelling corrections * review * improved documentation, removed redundant variables, added testing * added type hint * camel case to snake case * spelling fix * review * python --> Python # it is a brand name, not a snake * explicit cast to int * spaces in int list * "!= None" to "is not None" * Update comb_sort.py * various spelling corrections in documentation & several variables naming conventions fix * + char in file name * import dependency - bug fix Co-authored-by: John Law <johnlaw.po@gmail.com>
33 lines
846 B
Python
33 lines
846 B
Python
import collections, pprint, time, os
|
|
|
|
start_time = time.time()
|
|
print("creating word list...")
|
|
path = os.path.split(os.path.realpath(__file__))
|
|
with open(path[0] + "/words") as f:
|
|
word_list = sorted(list({word.strip().lower() for word in f}))
|
|
|
|
|
|
def signature(word):
|
|
return "".join(sorted(word))
|
|
|
|
|
|
word_bysig = collections.defaultdict(list)
|
|
for word in word_list:
|
|
word_bysig[signature(word)].append(word)
|
|
|
|
|
|
def anagram(my_word):
|
|
return word_bysig[signature(my_word)]
|
|
|
|
|
|
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))
|
|
|
|
total_time = round(time.time() - start_time, 2)
|
|
print(("Done [", total_time, "seconds ]"))
|