aboutsummaryrefslogtreecommitdiff
path: root/challenge-005/paulo-custodio/python/ch-2.py
blob: 366717d95f3a7663045874025ab9fd6cc06509ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3

# Challenge 005
#
# Challenge #2
# Write a program to find the sequence of characters that has the most anagrams.
#
# create a hash of all words in dictionary where key is sorted list of letters
# therefore two anagrams have the same key

import sys
import re

def read_file(filename):
    with open(filename) as f:
        return f.readlines()

def read_words(lines):
    words = []
    for line in lines:
        word = line.strip()
        if not re.search(r"\W", word):
            words.append(word)
    return words

def get_word_key(word):
    letters = sorted([x for x in word.lower()])
    return "".join(letters)

def print_largest_anagram():
    anagrams = {}
    max_anagrams = 0
    for word in read_words(read_file("words.txt")):
        if len(word) >= 2:
            key = get_word_key(word)
            if key in anagrams:
                anagrams[key] += 1
            else:
                anagrams[key] = 1
            max_anagrams = max(max_anagrams, anagrams[key])
    print(f"Maximum of {max_anagrams} anagrams")
    for key in sorted(anagrams):
        if anagrams[key]==max_anagrams:
            print(key)

print_largest_anagram()