4d47's solution

to OCR Numbers in the PHP Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a 3 x 4 grid of pipes, underscores, and spaces, determine which number is represented, or whether it is garbled.

Step One

To begin with, convert a simple binary font to a string containing 0 or 1.

The binary font uses pipes and underscores, four rows high and three columns wide.

``````     _   #
| |  # zero.
|_|  #
# the fourth row is always blank
``````

Is converted to "0"

``````         #
|  # one.
|  #
# (blank fourth row)
``````

Is converted to "1"

If the input is the correct size, but not recognizable, your program should return '?'

If the input is the incorrect size, your program should return an error.

Step Two

Update your program to recognize multi-character binary strings, replacing garbled numbers with ?

Step Three

Update your program to recognize all numbers 0 through 9, both individually and as part of a larger string.

`````` _
_|
|_

``````

Is converted to "2"

``````      _  _     _  _  _  _  _  _  #
| _| _||_||_ |_   ||_||_|| | # decimal numbers.
||_  _|  | _||_|  ||_| _||_| #
# fourth line is always blank
``````

Is converted to "1234567890"

Step Four

Update your program to handle multiple numbers, one per line. When converting several lines, join the lines with commas.

``````    _  _
| _| _|
||_  _|

_  _
|_||_ |_
| _||_|

_  _  _
||_||_|
||_| _|

``````

Is converted to "123,456,789"

Running the tests

1. Go to the root of your PHP exercise directory, which is `<EXERCISM_WORKSPACE>/php`. To find the Exercism workspace run

`````` % exercism debug | grep Workspace
``````
2. Get PHPUnit if you don't have it already.

`````` % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
% chmod +x phpunit.phar
``````
3. Execute the tests:

`````` % ./phpunit.phar ocr-numbers/ocr-numbers_test.php
``````

Source

Inspired by the Bank OCR kata http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ocr-numbers_test.php

``````<?php

include_once 'ocr-numbers.php';

class OcrNumbersTest extends PHPUnit\Framework\TestCase
{

/**
* Recognition result should be returned as a string
*/

public function testRecognizes0()
{
\$input = [
" _ ",
"| |",
"|_|",
"   ",
];
\$this->assertSame('0', recognize(\$input));
}

public function testRecognizes1()
{
\$input = [
"   ",
"  |",
"  |",
"   ",
];
\$this->assertSame('1', recognize(\$input));
}

/**
* Unreadable but correctly sized inputs return ?
*/
{
\$input = [
"   ",
"  _",
"  |",
"   ",
];
\$this->assertSame('?', recognize(\$input));
}

/**
* Input with a number of lines that is not a multiple of four raises an error
* @expectedException InvalidArgumentException
*/
public function testErrorWrongNumberOfLines()
{
\$input = [
" _ ",
"| |",
"   ",
];
recognize(\$input);
}

/**
* Input with a number of columns that is not a multiple of three raises an error
* @expectedException InvalidArgumentException
*/
public function testErrorWrongNumberOfColumns()
{
\$input = [
"    ",
"   |",
"   |",
"    ",
];
recognize(\$input);
}

public function testRecognizes110101100()
{
\$input = [
"       _     _        _  _ ",
"  |  || |  || |  |  || || |",
"  |  ||_|  ||_|  |  ||_||_|",
"                           ",
];
\$this->assertSame('110101100', recognize(\$input));
}

/**
* Garbled numbers in a string are replaced with ?
*/
public function testGarbled()
{
\$input = [
"       _     _           _ ",
"  |  || |  || |     || || |",
"  |  | _|  ||_|  |  ||_||_|",
"                           ",
];
\$this->assertSame('11?10?1?0', recognize(\$input));
}

public function testRecognizes2()
{
\$input = [
" _ ",
" _|",
"|_ ",
"   ",
];
\$this->assertSame('2', recognize(\$input));
}

public function testRecognizes3()
{
\$input = [
" _ ",
" _|",
" _|",
"   ",
];
\$this->assertSame('3', recognize(\$input));
}

public function testRecognizes4()
{
\$input = [
"   ",
"|_|",
"  |",
"   ",
];
\$this->assertSame('4', recognize(\$input));
}

public function testRecognizes5()
{
\$input = [
" _ ",
"|_ ",
" _|",
"   ",
];
\$this->assertSame('5', recognize(\$input));
}

public function testRecognizes6()
{
\$input = [
" _ ",
"|_ ",
"|_|",
"   ",
];
\$this->assertSame('6', recognize(\$input));
}

public function testRecognizes7()
{
\$input = [
" _ ",
"  |",
"  |",
"   ",
];
\$this->assertSame('7', recognize(\$input));
}

public function testRecognizes8()
{
\$input = [
" _ ",
"|_|",
"|_|",
"   ",
];
\$this->assertSame('8', recognize(\$input));
}

public function testRecognizes9()
{
\$input = [
" _ ",
"|_|",
" _|",
"   ",
];
\$this->assertSame('9', recognize(\$input));
}

public function testRecognizesStringOfDecimalNumbers()
{
\$input = [
"    _  _     _  _  _  _  _  _ ",
"  | _| _||_||_ |_   ||_||_|| |",
"  ||_  _|  | _||_|  ||_| _||_|",
"                              ",
];
\$this->assertSame('1234567890', recognize(\$input));
}

/**
* Numbers separated by empty lines are recognized. Lines are joined by commas.
*/
public function testLinesWithCommas()
{
\$input = [
"    _  _ ",
"  | _| _|",
"  ||_  _|",
"         ",
"    _  _ ",
"|_||_ |_ ",
"  | _||_|",
"         ",
" _  _  _ ",
"  ||_||_|",
"  ||_| _|",
"         ",
];
\$this->assertSame('123,456,789', recognize(\$input));
}
}``````
``````<?php

function recognize(array \$grid): string
{
if (count(\$grid) % 4 != 0) {
throw new InvalidArgumentException('Input \$grid must be divisible by four');
}
if (in_array(false, array_map(function(\$line) { return strlen(\$line) % 3 == 0; }, \$grid))) {
throw new InvalidArgumentException('\$grid lengths must be multiple of three');
}
return implode(',', array_map('lines2number', array_chunk(\$grid, 4)));
}

function lines2number(array \$digits): string
{
return implode(array_map('digit2char', digitize(\$digits)));
}

function digit2char(array \$digit): string
{
static \$digits;
if (!\$digits) {
\$digits = digitize([
' _     _  _     _  _  _  _  _ ',
'| |  | _| _||_||_ |_   ||_||_|',
'|_|  ||_  _|  | _||_|  ||_| _|',
'                              ',
]);
}
\$index = array_search(\$digit, \$digits);
return \$index !== false ? \$index : '?';
}

function digitize(array \$lines): array
{
\$digits = [];
for (\$i = 0; \$i < strlen(\$lines[0]); \$i += 3) {
\$digits[] = [];
foreach (\$lines as \$line) {
\$digits[count(\$digits) - 1][] = substr(\$line, \$i, 3);
}
}
return \$digits;
}``````