This repository was archived by the owner on Mar 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathocr_numbers_test.py
More file actions
122 lines (99 loc) · 3.68 KB
/
Copy pathocr_numbers_test.py
File metadata and controls
122 lines (99 loc) · 3.68 KB
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
113
114
115
116
117
118
119
120
121
122
import unittest
from ocr_numbers import convert
# Tests adapted from `problem-specifications//canonical-data.json`
class OcrNumbersTest(unittest.TestCase):
def test_recognizes_0(self):
self.assertEqual(convert([" _ ", "| |", "|_|", " "]), "0")
def test_recognizes_1(self):
self.assertEqual(convert([" ", " |", " |", " "]), "1")
def test_unreadable_but_correctly_sized_inputs_return(self):
self.assertEqual(convert([" ", " _", " |", " "]), "?")
def test_input_with_a_number_of_lines_that_is_not_a_multiple_of_four_raises_an_error(
self
):
with self.assertRaisesWithMessage(ValueError):
convert([" _ ", "| |", " "])
def test_input_with_a_number_of_columns_that_is_not_a_multiple_of_three_raises_an_error(
self
):
with self.assertRaisesWithMessage(ValueError):
convert([" ", " |", " |", " "])
def test_recognizes_110101100(self):
self.assertEqual(
convert(
[
" _ _ _ _ ",
" | || | || | | || || |",
" | ||_| ||_| | ||_||_|",
" ",
]
),
"110101100",
)
def test_garbled_numbers_in_a_string_are_replaced_with(self):
self.assertEqual(
convert(
[
" _ _ _ ",
" | || | || | || || |",
" | | _| ||_| | ||_||_|",
" ",
]
),
"11?10?1?0",
)
def test_recognizes_2(self):
self.assertEqual(convert([" _ ", " _|", "|_ ", " "]), "2")
def test_recognizes_3(self):
self.assertEqual(convert([" _ ", " _|", " _|", " "]), "3")
def test_recognizes_4(self):
self.assertEqual(convert([" ", "|_|", " |", " "]), "4")
def test_recognizes_5(self):
self.assertEqual(convert([" _ ", "|_ ", " _|", " "]), "5")
def test_recognizes_6(self):
self.assertEqual(convert([" _ ", "|_ ", "|_|", " "]), "6")
def test_recognizes_7(self):
self.assertEqual(convert([" _ ", " |", " |", " "]), "7")
def test_recognizes_8(self):
self.assertEqual(convert([" _ ", "|_|", "|_|", " "]), "8")
def test_recognizes_9(self):
self.assertEqual(convert([" _ ", "|_|", " _|", " "]), "9")
def test_recognizes_string_of_decimal_numbers(self):
self.assertEqual(
convert(
[
" _ _ _ _ _ _ _ _ ",
" | _| _||_||_ |_ ||_||_|| |",
" ||_ _| | _||_| ||_| _||_|",
" ",
]
),
"1234567890",
)
def test_numbers_separated_by_empty_lines_are_recognized_lines_are_joined_by_commas(
self
):
self.assertEqual(
convert(
[
" _ _ ",
" | _| _|",
" ||_ _|",
" ",
" _ _ ",
"|_||_ |_ ",
" | _||_|",
" ",
" _ _ _ ",
" ||_||_|",
" ||_| _|",
" ",
]
),
"123,456,789",
)
# Utility functions
def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")
if __name__ == "__main__":
unittest.main()