Anagrams
A solution to Anagrams kata described at CodeKata.
Solution
""" anagrams.py """
# usage: python3 anagrams.py
with open('wordlist-utf8.txt', 'r', encoding='utf-8') as text_input:
words = text_input.read().split()
anagrams = {}
for word in words:
key = ''.join(sorted(word))
values = anagrams.get(key, [])
values.append(word)
anagrams[key] = values
sets = 0
total_words = 0
for _, values in anagrams.items():
if len(values) > 1:
sets += 1
total_words += len(values)
print(f'Found {sets} sets of anagrams (a total of {total_words} words).')
# --
longest = {}
max_key_length = 0
for key, values in anagrams.items():
if len(values) <= 1:
continue
key_length = len(key)
if key_length > max_key_length:
longest.clear()
longest[key] = values
max_key_length = len(key)
elif key_length == max_key_length:
longest[key] = values
print('\nLongest word anagrams')
for _, values in longest.items():
print(values)
# --
most = {}
max_words_length = 0
for key, values in anagrams.items():
words_length = len(values)
if words_length > max_words_length:
most.clear()
most[key] = values
max_words_length = words_length
elif words_length == max_words_length:
most[key] = values
print('\nMost words anagrams')
for _, values in most.items():
print(values)