Skip to content

Commit 6b02dc1

Browse files
emeraliilya-khadykin
authored andcommitted
ocr-numbers: Update tests to reflect changes to canonical data (exercism#1071)
1 parent ee8c719 commit 6b02dc1

3 files changed

Lines changed: 143 additions & 132 deletions

File tree

exercises/ocr-numbers/example.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
ROW = 4
2-
COL = 3
1+
NUM_ROWS = 4
2+
NUM_COLS = 3
33

44

55
def split_ocr(ocr):
6-
return [[ocr[i][COL * j:COL * (j + 1)] for i in range(ROW)]
7-
for j in range(len(ocr[0]) // COL)]
6+
return [[ocr[i][NUM_COLS * j:NUM_COLS * (j + 1)] for i in range(NUM_ROWS)]
7+
for j in range(len(ocr[0]) // NUM_COLS)]
88

99

1010
ALL = [' _ _ _ _ _ _ _ _ ',
@@ -16,28 +16,25 @@ def split_ocr(ocr):
1616
OCR_LIST = [OCR_LIST[-1]] + OCR_LIST[:9]
1717

1818

19-
def number(ocr):
20-
if (len(ocr) != ROW or len(ocr[0]) % COL or
21-
any(len(r) != len(ocr[0]) for r in ocr)):
19+
def convert(input_grid):
20+
split_indices = (list(range(0, len(input_grid), NUM_ROWS)) +
21+
[len(input_grid)])
22+
23+
lines = [input_grid[start:end]
24+
for start, end in zip(split_indices[:-1], split_indices[1:])]
25+
26+
return ",".join(convert_one_line(line) for line in lines)
27+
28+
29+
def convert_one_line(input_grid):
30+
if (len(input_grid) != NUM_ROWS or len(input_grid[0]) % NUM_COLS or
31+
any(len(r) != len(input_grid[0]) for r in input_grid)):
2232
raise ValueError('Wrong grid size.')
23-
numbers = split_ocr(ocr)
33+
numbers = split_ocr(input_grid)
2434
digits = ''
2535
for n in numbers:
2636
try:
2737
digits += str(OCR_LIST.index(n))
2838
except ValueError:
2939
digits += '?'
3040
return digits
31-
32-
33-
def grid(digits):
34-
try:
35-
if not digits.isdigit():
36-
raise ValueError('String should be numeric.')
37-
except AttributeError:
38-
raise ValueError('Argument should be a string.')
39-
ocr = ['' for i in range(ROW)]
40-
for d in digits:
41-
for r in range(ROW):
42-
ocr[r] += OCR_LIST[int(d)][r]
43-
return ocr
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
def number(digits):
2-
pass
3-
4-
5-
def grid(digits):
1+
def convert(input_grid):
62
pass

exercises/ocr-numbers/ocr_numbers_test.py

Lines changed: 124 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,141 @@
11
"""Tests for the ocr-numbers exercise
22
33
Implementation note:
4-
Both ocr.grid and ocr.number should validate their input
5-
and raise ValueErrors with meaningful error messages
4+
ocr.convert should validate its input and
5+
raise ValueErrors with meaningful error messages
66
if necessary.
77
"""
88

99
import unittest
1010

11-
from ocr_numbers import grid, number
11+
from ocr_numbers import convert
1212

1313

14+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
15+
1416
class OcrTest(unittest.TestCase):
15-
def test_0(self):
16-
self.assertEqual(number([" _ ",
17-
"| |",
18-
"|_|",
19-
" "]), '0')
20-
21-
def test_1(self):
22-
self.assertEqual(number([" ",
23-
" |",
24-
" |",
25-
" "]), '1')
26-
27-
def test_garbage(self):
28-
self.assertEqual(number([" _ ",
29-
" _|",
30-
" |",
31-
" "]), '?')
32-
33-
def test_last_line_nonblank(self):
34-
self.assertEqual(number([" ",
35-
" |",
36-
" |",
37-
"| |"]), '?')
38-
39-
def test_unknown_char(self):
40-
self.assertEqual(number([" - ",
41-
" _|",
42-
" X|",
43-
" "]), '?')
44-
45-
def test_too_short_row(self):
17+
def test_recognizes_0(self):
18+
self.assertEqual(convert([" _ ",
19+
"| |",
20+
"|_|",
21+
" "]), '0')
22+
23+
def test_recognizes_1(self):
24+
self.assertEqual(convert([" ",
25+
" |",
26+
" |",
27+
" "]), '1')
28+
29+
def test_unreadable(self):
30+
self.assertEqual(convert([" ",
31+
" _",
32+
" |",
33+
" "]), '?')
34+
35+
def test_line_number_not_multiple_of_four(self):
4636
with self.assertRaises(ValueError):
47-
number([" ",
48-
" _|",
49-
" |",
50-
" "])
37+
convert([" _ ",
38+
"| |",
39+
" "])
5140

52-
def test_insufficient_rows(self):
53-
with self.assertRaises(ValueError):
54-
number([" ",
55-
" _|",
56-
" X|"])
57-
58-
def test_grid0(self):
59-
self.assertEqual(grid('0'), [" _ ",
60-
"| |",
61-
"|_|",
62-
" "])
63-
64-
def test_grid1(self):
65-
self.assertEqual(grid('1'), [" ",
66-
" |",
67-
" |",
68-
" "])
69-
70-
def test_0010110(self):
71-
self.assertEqual(
72-
number([
73-
" _ _ _ _ ",
74-
"| || | || | | || |",
75-
"|_||_| ||_| | ||_|",
76-
" "
77-
]), '0010110')
78-
79-
def test_3186547290(self):
80-
digits = '3186547290'
81-
self.assertEqual(
82-
number([
83-
" _ _ _ _ _ _ _ _ ",
84-
" _| ||_||_ |_ |_| | _||_|| |",
85-
" _| ||_||_| _| | ||_ _||_|",
86-
" "
87-
]), digits)
88-
89-
def test_Lost(self):
90-
digits = '4815162342'
91-
self.assertEqual(
92-
number([
93-
" _ _ _ _ _ _ ",
94-
"|_||_| ||_ ||_ _| _||_| _|",
95-
" ||_| | _| ||_||_ _| ||_ ",
96-
" "
97-
]), digits)
98-
99-
def test_garble_middle(self):
100-
self.assertEqual(
101-
number([
102-
" _ _ _ ",
103-
" | _| ||_||_ ",
104-
" ||_ _| | _|",
105-
" "
106-
]), '12?45')
107-
108-
def test_grid3186547290(self):
109-
digits = '3186547290'
110-
self.assertEqual(
111-
grid(digits), [
112-
" _ _ _ _ _ _ _ _ ",
113-
" _| ||_||_ |_ |_| | _||_|| |",
114-
" _| ||_||_| _| | ||_ _||_|",
115-
" "
116-
])
117-
118-
def test_invalid_grid(self):
41+
def test_col_number_not_multiple_of_three(self):
11942
with self.assertRaises(ValueError):
120-
grid('123a')
43+
convert([" ",
44+
" |",
45+
" |",
46+
" "])
47+
48+
def test_recognizes_110101100(self):
49+
input_grid = [
50+
" _ _ _ _ ",
51+
" | || | || | | || || |",
52+
" | ||_| ||_| | ||_||_|",
53+
" "
54+
]
55+
self.assertEqual(convert(input_grid), "110101100")
56+
57+
def test_garbled_numbers_in_string(self):
58+
input_grid = [
59+
" _ _ _ ",
60+
" | || | || | || || |",
61+
" | | _| ||_| | ||_||_|",
62+
" "
63+
]
64+
self.assertEqual(convert(input_grid), "11?10?1?0")
65+
66+
def test_recognizes_2(self):
67+
self.assertEqual(convert([" _ ",
68+
" _|",
69+
"|_ ",
70+
" "]), "2")
71+
72+
def test_recognizes_3(self):
73+
self.assertEqual(convert([" _ ",
74+
" _|",
75+
" _|",
76+
" "]), "3")
77+
78+
def test_recognizes_4(self):
79+
self.assertEqual(convert([" ",
80+
"|_|",
81+
" |",
82+
" "]), "4")
83+
84+
def test_recognizes_5(self):
85+
self.assertEqual(convert([" _ ",
86+
"|_ ",
87+
" _|",
88+
" "]), "5")
89+
90+
def test_recognizes_6(self):
91+
self.assertEqual(convert([" _ ",
92+
"|_ ",
93+
"|_|",
94+
" "]), "6")
95+
96+
def test_recognizes_7(self):
97+
self.assertEqual(convert([" _ ",
98+
" |",
99+
" |",
100+
" "]), "7")
101+
102+
def test_recognizes_8(self):
103+
self.assertEqual(convert([" _ ",
104+
"|_|",
105+
"|_|",
106+
" "]), "8")
107+
108+
def test_recognizes_9(self):
109+
self.assertEqual(convert([" _ ",
110+
"|_|",
111+
" _|",
112+
" "]), "9")
113+
114+
def test_recognizes_string_of_decimal_numbers(self):
115+
input_grid = [
116+
" _ _ _ _ _ _ _ _ ",
117+
" | _| _||_||_ |_ ||_||_|| |",
118+
" ||_ _| | _||_| ||_| _||_|",
119+
" "
120+
]
121+
self.assertEqual(convert(input_grid), "1234567890")
122+
123+
def test_recognizes_numbers_separated_by_empty_lines(self):
124+
input_grid = [
125+
" _ _ ",
126+
" | _| _|",
127+
" ||_ _|",
128+
" ",
129+
" _ _ ",
130+
"|_||_ |_ ",
131+
" | _||_|",
132+
" ",
133+
" _ _ _ ",
134+
" ||_||_|",
135+
" ||_| _|",
136+
" "
137+
]
138+
self.assertEqual(convert(input_grid), "123,456,789")
121139

122140

123141
if __name__ == '__main__':

0 commit comments

Comments
 (0)