Sha256: cb6f25315d6c2c42e4e4a55b412073a983891364a033fc242f0c2ce826fe55a4

Contents?: true

Size: 1.12 KB

Versions: 116

Compression:

Stored size: 1.12 KB

Contents

NUM_ROWS = 4
NUM_COLS = 3


def split_ocr(ocr):
    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 = ['    _  _     _  _  _  _  _  _ ',
       '  | _| _||_||_ |_   ||_||_|| |',
       '  ||_  _|  | _||_|  ||_| _||_|',
       '                              ']

OCR_LIST = split_ocr(ALL)
OCR_LIST = [OCR_LIST[-1]] + OCR_LIST[:9]


def convert(input_grid):
    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(input_grid)
    digits = ''
    for n in numbers:
        try:
            digits += str(OCR_LIST.index(n))
        except ValueError:
            digits += '?'
    return digits

Version data entries

116 entries across 116 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.179 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.178 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.177 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.176 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.175 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.174 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.173 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.172 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.171 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.170 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.169 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.167 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.166 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.165 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.164 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.163 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.162 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.161 tracks/python/exercises/ocr-numbers/example.py
trackler-2.2.1.160 tracks/python/exercises/ocr-numbers/example.py