aboutsummaryrefslogtreecommitdiff
path: root/challenge-161/lubos-kolouch/python/ch-2.py
blob: 7936fc91179182b5a3f77516167ceaa0d3b746ca (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
47
48
49
50
51
52
""" Challenge 161 LK Python """


class Pangram:
    """Challenge 161 Task 2"""

    def __init__(self) -> None:
        self.seen_letters: dict = {}
        self.output: list = []

    @staticmethod
    def is_abcd(what: str) -> bool:
        """Check if the file is in the right order"""
        return what == "".join(sorted(what))

    def process_word(self, what: str) -> None:
        """Check if the word matches"""

        this_run_letters = dict(self.seen_letters)

        for char in what:
            this_run_letters[char] = 1

        if len(self.seen_letters.keys()) == len(this_run_letters.keys()) - 1:
            # we added a letter
            self.output.append(what)
            self.seen_letters = dict(this_run_letters)

        return

    def get_abcdrian_pangram(self) -> str:
        """only abecederian words solving exactly one letter"""
        with open("dictionary.txt") as in_file:

            while line := in_file.readline().strip():
                if not self.is_abcd(line):
                    continue

                self.process_word(line)

                # seen all letters?
                if len(self.seen_letters.keys()) == 26:
                    return " ".join(self.output)

        return ""


pangram = Pangram()
assert (
    pangram.get_abcdrian_pangram()
    == "a ad adds ado ago ah ahoy all allot allow almost amp an art ax blot buy buzz cc cell chi deft envy jot knot qt"
)