1
exercism fetch python alphametics

alphametics_test.py

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import unittest

from alphametics import solve


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0

class AlphameticsTest(unittest.TestCase):
    def test_puzzle_with_three_letters(self):
        self.assertEqual(solve("I + BB == ILL"), {"I": 1, "B": 9, "L": 0})

    def test_solution_must_have_unique_value_for_each_letter(self):
        self.assertEqual(solve("A == B"), {})

    def test_leading_zero_solution_is_invalid(self):
        self.assertEqual(solve("ACA + DD == BD"), {})

    def test_puzzle_with_four_letters(self):
        self.assertEqual(
            solve("AS + A == MOM"), {"A": 9, "S": 2, "M": 1, "O": 0})

    def test_puzzle_with_six_letters(self):
        self.assertEqual(
            solve("NO + NO + TOO == LATE"),
            {"N": 7,
             "O": 4,
             "T": 9,
             "L": 1,
             "A": 0,
             "E": 2})

    def test_puzzle_with_seven_letters(self):
        self.assertEqual(
            solve("HE + SEES + THE == LIGHT"),
            {"E": 4,
             "G": 2,
             "H": 5,
             "I": 0,
             "L": 1,
             "S": 9,
             "T": 7})

    def test_puzzle_with_eight_letters(self):
        self.assertEqual(
            solve("SEND + MORE == MONEY"),
            {"S": 9,
             "E": 5,
             "N": 6,
             "D": 7,
             "M": 1,
             "O": 0,
             "R": 8,
             "Y": 2})

    def test_puzzle_with_ten_letters(self):
        self.assertEqual(
            solve("AND + A + STRONG + OFFENSE + AS + A + GOOD == DEFENSE"),
            {"A": 5,
             "D": 3,
             "E": 4,
             "F": 7,
             "G": 8,
             "N": 0,
             "O": 2,
             "R": 1,
             "S": 6,
             "T": 9})

    @unittest.skip("extra-credit")
    def test_puzzle_with_ten_letters_and_199_addends(self):
        self.assertEqual(
            solve(
                "THIS + A + FIRE + THEREFORE + FOR + ALL + HISTORIES + I + "
                "TELL + A + TALE + THAT + FALSIFIES + ITS + TITLE + TIS + "
                "A + LIE + THE + TALE + OF + THE + LAST + FIRE + HORSES + "
                "LATE + AFTER + THE + FIRST + FATHERS + FORESEE + THE + "
                "HORRORS + THE + LAST + FREE + TROLL + TERRIFIES + THE + "
                "HORSES + OF + FIRE + THE + TROLL + RESTS + AT + THE + "
                "HOLE + OF + LOSSES + IT + IS + THERE + THAT + SHE + STORES + "
                "ROLES + OF + LEATHERS + AFTER + SHE + SATISFIES + HER + "
                "HATE + OFF + THOSE + FEARS + A + TASTE + RISES + AS + SHE + "
                "HEARS + THE + LEAST + FAR + HORSE + THOSE + FAST + HORSES + "
                "THAT + FIRST + HEAR + THE + TROLL + FLEE + OFF + TO + THE + "
                "FOREST + THE + HORSES + THAT + ALERTS + RAISE + THE + STARES "
                "+ OF + THE + OTHERS + AS + THE + TROLL + ASSAILS + AT + THE "
                "+ TOTAL + SHIFT + HER + TEETH + TEAR + HOOF + OFF + TORSO + "
                "AS + THE + LAST + HORSE + FORFEITS + ITS + LIFE + THE + "
                "FIRST + FATHERS + HEAR + OF + THE + HORRORS + THEIR + FEARS "
                "+ THAT + THE + FIRES + FOR + THEIR + FEASTS + ARREST + AS + "
                "THE + FIRST + FATHERS + RESETTLE + THE + LAST + OF + THE + "
                "FIRE + HORSES + THE + LAST + TROLL + HARASSES + THE + FOREST "
                "+ HEART + FREE + AT + LAST + OF + THE + LAST + TROLL + ALL + "
                "OFFER + THEIR + FIRE + HEAT + TO + THE + ASSISTERS + FAR + "
                "OFF + THE + TROLL + FASTS + ITS + LIFE + SHORTER + AS + "
                "STARS + RISE + THE + HORSES + REST + SAFE + AFTER + ALL + "
                "SHARE + HOT + FISH + AS + THEIR + AFFILIATES + TAILOR + A + "
                "ROOFS + FOR + THEIR + SAFE == FORTRESSES"
            ),
            {"A": 1,
             "E": 0,
             "F": 5,
             "H": 8,
             "I": 7,
             "L": 2,
             "O": 6,
             "R": 3,
             "S": 4,
             "T": 9})


if __name__ == '__main__':
    unittest.main()