Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions bin/auto-sync.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ matching-brackets
meetup
micro-blog
nucleotide-count
ocr-numbers
pangram
pascals-triangle
phone-number
Expand Down
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@
"uuid": "a7a269a5-e99c-4fcf-b331-9a130e94f9e4",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"difficulty": 5,
"topics": [
"algorithms",
"strings",
Expand Down
80 changes: 24 additions & 56 deletions exercises/practice/ocr-numbers/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -1,79 +1,47 @@
# Instructions

Given a 3 x 4 grid of pipes, underscores, and spaces, determine which number is represented, or whether it is garbled.
Optical Character Recognition or OCR is software that converts images of text into machine-readable text.
Given a grid of characters representing some digits, convert the grid to a string of digits.
If the grid has multiple rows of cells, the rows should be separated in the output with a `","`.

## Step One
- The grid is made of one of more lines of cells.
- Each line of the grid is made of one or more cells.
- Each cell is three columns wide and four rows high (3x4) and represents one digit.
- Digits are drawn using pipes (`"|"`), underscores (`"_"`), and spaces (`" "`).

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

The binary font uses pipes and underscores, four rows high and three columns wide.
- If the input is not a valid size, your program should indicate there is an error.
- If the input is the correct size, but a cell is not recognizable, your program should output a `"?"` for that character.

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

Is converted to "0"

```text
#
| # 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.

```text
_
_|
|_

```

Is converted to "2"
The following input (without the comments) is converted to `"1234567890"`.

```text
_ _ _ _ _ _ _ _ #
| _| _||_||_ |_ ||_||_|| | # decimal numbers.
| _| _||_||_ |_ ||_||_|| | # Decimal numbers.
||_ _| | _||_| ||_| _||_| #
# fourth line is always blank
# The fourth line is always blank,
```

Is converted to "1234567890"

## Step Four
The following input is converted to `"123,456,789"`.

Update your program to handle multiple numbers, one per line.
When converting several lines, join the lines with commas.
<!-- prettier-ignore-start -->

```text
_ _
_ _
| _| _|
||_ _|

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

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

```

Is converted to "123,456,789".
<!-- prettier-ignore-end -->
5 changes: 3 additions & 2 deletions exercises/practice/ocr-numbers/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"arueckauer",
"G-Rath",
"kytrinyx",
"petemcfarlane"
"petemcfarlane",
"mk-mxp"
],
"files": {
"solution": [
Expand All @@ -21,5 +22,5 @@
},
"blurb": "Given a 3 x 4 grid of pipes, underscores, and spaces, determine which number is represented, or whether it is garbled.",
"source": "Inspired by the Bank OCR kata",
"source_url": "https://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR"
"source_url": "https://codingdojo.org/kata/BankOCR/"
}
22 changes: 0 additions & 22 deletions exercises/practice/ocr-numbers/.meta/example.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

function recognize($ocr)
Expand Down
96 changes: 62 additions & 34 deletions exercises/practice/ocr-numbers/OcrNumbersTest.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

class OcrNumbersTest extends TestCase
Expand All @@ -34,9 +13,9 @@ public static function setUpBeforeClass(): void
}

/**
* Recognition result should be returned as a string
* uuid: 5ee54e1a-b554-4bf3-a056-9a7976c3f7e8
*/

#[TestDox('Recognizes 0')]
public function testRecognizes0(): void
{
$input = [
Expand All @@ -48,6 +27,10 @@ public function testRecognizes0(): void
$this->assertSame('0', recognize($input));
}

/**
* uuid: 027ada25-17fd-4d78-aee6-35a19623639d
*/
#[TestDox('Recognizes 1')]
public function testRecognizes1(): void
{
$input = [
Expand All @@ -60,9 +43,10 @@ public function testRecognizes1(): void
}

/**
* Unreadable but correctly sized inputs return ?
* uuid: 3cce2dbd-01d9-4f94-8fae-419a822e89bb
*/
public function testUnreadable(): void
#[TestDox('Unreadable but correctly sized inputs return ?')]
public function testUnreadableButCorrectlySizedInputsReturnQuestionmark(): void
{
$input = [
" ",
Expand All @@ -74,9 +58,10 @@ public function testUnreadable(): void
}

/**
* Input with a number of lines that is not a multiple of four raises an error
* uuid: cb19b733-4e36-4cf9-a4a1-6e6aac808b9a
*/
public function testErrorWrongNumberOfLines(): void
#[TestDox('Input with a number of lines that is not a multiple of four raises an error')]
public function testInputWithANumberOfLinesThatIsNotAMultipleOfFourRaisesAnError(): void
{
$this->expectException(InvalidArgumentException::class);

Expand All @@ -89,9 +74,10 @@ public function testErrorWrongNumberOfLines(): void
}

/**
* Input with a number of columns that is not a multiple of three raises an error
* uuid: 235f7bd1-991b-4587-98d4-84206eec4cc6
*/
public function testErrorWrongNumberOfColumns(): void
#[TestDox('Input with a number of columns that is not a multiple of three raises an error')]
public function testInputWithANumberOfColumnsThatIsNotAMultipleOfThreeRaisesAnError(): void
{
$this->expectException(InvalidArgumentException::class);

Expand All @@ -104,6 +90,10 @@ public function testErrorWrongNumberOfColumns(): void
recognize($input);
}

/**
* uuid: 4a841794-73c9-4da9-a779-1f9837faff66
*/
#[TestDox('Recognizes 110101100')]
public function testRecognizes110101100(): void
{
$input = [
Expand All @@ -116,9 +106,10 @@ public function testRecognizes110101100(): void
}

/**
* Garbled numbers in a string are replaced with ?
* uuid: 70c338f9-85b1-4296-a3a8-122901cdfde8
*/
public function testGarbled(): void
#[TestDox('Garbled numbers in a string are replaced with ?')]
public function testGarbledNumbersInAStringAreReplacedWithQuestionmark(): void
{
$input = [
" _ _ _ ",
Expand All @@ -129,6 +120,10 @@ public function testGarbled(): void
$this->assertSame('11?10?1?0', recognize($input));
}

/**
* uuid: ea494ff4-3610-44d7-ab7e-72fdef0e0802
*/
#[TestDox('Recognizes 2')]
public function testRecognizes2(): void
{
$input = [
Expand All @@ -140,6 +135,10 @@ public function testRecognizes2(): void
$this->assertSame('2', recognize($input));
}

/**
* uuid: 1acd2c00-412b-4268-93c2-bd7ff8e05a2c
*/
#[TestDox('Recognizes 3')]
public function testRecognizes3(): void
{
$input = [
Expand All @@ -151,6 +150,10 @@ public function testRecognizes3(): void
$this->assertSame('3', recognize($input));
}

/**
* uuid: eaec6a15-be17-4b6d-b895-596fae5d1329
*/
#[TestDox('Recognizes 4')]
public function testRecognizes4(): void
{
$input = [
Expand All @@ -162,6 +165,10 @@ public function testRecognizes4(): void
$this->assertSame('4', recognize($input));
}

/**
* uuid: 440f397a-f046-4243-a6ca-81ab5406c56e
*/
#[TestDox('Recognizes 5')]
public function testRecognizes5(): void
{
$input = [
Expand All @@ -173,6 +180,10 @@ public function testRecognizes5(): void
$this->assertSame('5', recognize($input));
}

/**
* uuid: f4c9cf6a-f1e2-4878-bfc3-9b85b657caa0
*/
#[TestDox('Recognizes 6')]
public function testRecognizes6(): void
{
$input = [
Expand All @@ -184,6 +195,10 @@ public function testRecognizes6(): void
$this->assertSame('6', recognize($input));
}

/**
* uuid: e24ebf80-c611-41bb-a25a-ac2c0f232df5
*/
#[TestDox('Recognizes 7')]
public function testRecognizes7(): void
{
$input = [
Expand All @@ -195,6 +210,10 @@ public function testRecognizes7(): void
$this->assertSame('7', recognize($input));
}

/**
* uuid: b79cad4f-e264-4818-9d9e-77766792e233
*/
#[TestDox('Recognizes 8')]
public function testRecognizes8(): void
{
$input = [
Expand All @@ -206,6 +225,10 @@ public function testRecognizes8(): void
$this->assertSame('8', recognize($input));
}

/**
* uuid: 5efc9cfc-9227-4688-b77d-845049299e66
*/
#[TestDox('Recognizes 9')]
public function testRecognizes9(): void
{
$input = [
Expand All @@ -217,6 +240,10 @@ public function testRecognizes9(): void
$this->assertSame('9', recognize($input));
}

/**
* uuid: f60cb04a-42be-494e-a535-3451c8e097a4
*/
#[TestDox('Recognizes string of decimal numbers')]
public function testRecognizesStringOfDecimalNumbers(): void
{
$input = [
Expand All @@ -229,9 +256,10 @@ public function testRecognizesStringOfDecimalNumbers(): void
}

/**
* Numbers separated by empty lines are recognized. Lines are joined by commas.
* uuid: b73ecf8b-4423-4b36-860d-3710bdb8a491
*/
public function testLinesWithCommas(): void
#[TestDox('Numbers separated by empty lines are recognized. Lines are joined by commas.')]
public function testNumbersSeparatedByEmptyLinesAreRecognizedLinesAreJoinedByCommas(): void
{
$input = [
" _ _ ",
Expand Down
Loading