Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ocr-numbers: Update ocr-numbers to reflect changes to canonical data #1071

Merged
merged 3 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions exercises/ocr-numbers/example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
ROW = 4
COL = 3
NUM_ROWS = 4
NUM_COLS = 3


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


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


def number(ocr):
if (len(ocr) != ROW or len(ocr[0]) % COL or
any(len(r) != len(ocr[0]) for r in ocr)):
def convert(input_grid):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exercise placeholder should be also updated in this case:
https://github.com/exercism/python/blob/master/exercises/ocr-numbers/ocr_numbers.py

split_indices = (list(range(0, len(input_grid), NUM_ROWS)) +
[len(input_grid)])

lines = [input_grid[start:end]
for start, end in zip(split_indices[:-1], split_indices[1:])]

return ",".join(convert_one_line(line) for line in lines)


def convert_one_line(input_grid):
if (len(input_grid) != NUM_ROWS or len(input_grid[0]) % NUM_COLS or
any(len(r) != len(input_grid[0]) for r in input_grid)):
raise ValueError('Wrong grid size.')
numbers = split_ocr(ocr)
numbers = split_ocr(input_grid)
digits = ''
for n in numbers:
try:
digits += str(OCR_LIST.index(n))
except ValueError:
digits += '?'
return digits


def grid(digits):
try:
if not digits.isdigit():
raise ValueError('String should be numeric.')
except AttributeError:
raise ValueError('Argument should be a string.')
ocr = ['' for i in range(ROW)]
for d in digits:
for r in range(ROW):
ocr[r] += OCR_LIST[int(d)][r]
return ocr
230 changes: 124 additions & 106 deletions exercises/ocr-numbers/ocr_numbers_test.py
Original file line number Diff line number Diff line change
@@ -1,123 +1,141 @@
"""Tests for the ocr-numbers exercise

Implementation note:
Both ocr.grid and ocr.number should validate their input
and raise ValueErrors with meaningful error messages
ocr.convert should validate its input and
raise ValueErrors with meaningful error messages
if necessary.
"""

import unittest

from ocr_numbers import grid, number
from ocr_numbers import convert


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

class OcrTest(unittest.TestCase):
def test_0(self):
self.assertEqual(number([" _ ",
"| |",
"|_|",
" "]), '0')

def test_1(self):
self.assertEqual(number([" ",
" |",
" |",
" "]), '1')

def test_garbage(self):
self.assertEqual(number([" _ ",
" _|",
" |",
" "]), '?')

def test_last_line_nonblank(self):
self.assertEqual(number([" ",
" |",
" |",
"| |"]), '?')

def test_unknown_char(self):
self.assertEqual(number([" - ",
" _|",
" X|",
" "]), '?')

def test_too_short_row(self):
def test_recognizes_0(self):
self.assertEqual(convert([" _ ",
"| |",
"|_|",
" "]), '0')

def test_recognizes_1(self):
self.assertEqual(convert([" ",
" |",
" |",
" "]), '1')

def test_unreadable(self):
self.assertEqual(convert([" ",
" _",
" |",
" "]), '?')

def test_line_number_not_multiple_of_four(self):
with self.assertRaises(ValueError):
number([" ",
" _|",
" |",
" "])
convert([" _ ",
"| |",
" "])

def test_insufficient_rows(self):
with self.assertRaises(ValueError):
number([" ",
" _|",
" X|"])

def test_grid0(self):
self.assertEqual(grid('0'), [" _ ",
"| |",
"|_|",
" "])

def test_grid1(self):
self.assertEqual(grid('1'), [" ",
" |",
" |",
" "])

def test_0010110(self):
self.assertEqual(
number([
" _ _ _ _ ",
"| || | || | | || |",
"|_||_| ||_| | ||_|",
" "
]), '0010110')

def test_3186547290(self):
digits = '3186547290'
self.assertEqual(
number([
" _ _ _ _ _ _ _ _ ",
" _| ||_||_ |_ |_| | _||_|| |",
" _| ||_||_| _| | ||_ _||_|",
" "
]), digits)

def test_Lost(self):
digits = '4815162342'
self.assertEqual(
number([
" _ _ _ _ _ _ ",
"|_||_| ||_ ||_ _| _||_| _|",
" ||_| | _| ||_||_ _| ||_ ",
" "
]), digits)

def test_garble_middle(self):
self.assertEqual(
number([
" _ _ _ ",
" | _| ||_||_ ",
" ||_ _| | _|",
" "
]), '12?45')

def test_grid3186547290(self):
digits = '3186547290'
self.assertEqual(
grid(digits), [
" _ _ _ _ _ _ _ _ ",
" _| ||_||_ |_ |_| | _||_|| |",
" _| ||_||_| _| | ||_ _||_|",
" "
])

def test_invalid_grid(self):
def test_col_number_not_multiple_of_three(self):
with self.assertRaises(ValueError):
grid('123a')
convert([" ",
" |",
" |",
" "])

def test_recognizes_110101100(self):
input_grid = [
" _ _ _ _ ",
" | || | || | | || || |",
" | ||_| ||_| | ||_||_|",
" "
]
self.assertEqual(convert(input_grid), "110101100")

def test_garbled_numbers_in_string(self):
input_grid = [
" _ _ _ ",
" | || | || | || || |",
" | | _| ||_| | ||_||_|",
" "
]
self.assertEqual(convert(input_grid), "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):
input_grid = [
" _ _ _ _ _ _ _ _ ",
" | _| _||_||_ |_ ||_||_|| |",
" ||_ _| | _||_| ||_| _||_|",
" "
]
self.assertEqual(convert(input_grid), "1234567890")

def test_recognizes_numbers_separated_by_empty_lines(self):
input_grid = [
" _ _ ",
" | _| _|",
" ||_ _|",
" ",
" _ _ ",
"|_||_ |_ ",
" | _||_|",
" ",
" _ _ _ ",
" ||_||_|",
" ||_| _|",
" "
]
self.assertEqual(convert(input_grid), "123,456,789")


if __name__ == '__main__':
Expand Down