Skip to content

Commit 813ecdf

Browse files
authored
Sync ocr-numbers (#940)
1 parent 2042dc9 commit 813ecdf

6 files changed

Lines changed: 91 additions & 115 deletions

File tree

bin/auto-sync.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ matching-brackets
4949
meetup
5050
micro-blog
5151
nucleotide-count
52+
ocr-numbers
5253
pangram
5354
pascals-triangle
5455
phone-number

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@
953953
"uuid": "a7a269a5-e99c-4fcf-b331-9a130e94f9e4",
954954
"practices": [],
955955
"prerequisites": [],
956-
"difficulty": 3,
956+
"difficulty": 5,
957957
"topics": [
958958
"algorithms",
959959
"strings",
Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,47 @@
11
# Instructions
22

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

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

7-
To begin with, convert a simple binary font to a string containing 0 or 1.
12+
## Edge cases
813

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

11-
```text
12-
_ #
13-
| | # zero.
14-
|_| #
15-
# the fourth row is always blank
16-
```
17+
## Examples
1718

18-
Is converted to "0"
19-
20-
```text
21-
#
22-
| # one.
23-
| #
24-
# (blank fourth row)
25-
```
26-
27-
Is converted to "1"
28-
29-
If the input is the correct size, but not recognizable, your program should return '?'
30-
31-
If the input is the incorrect size, your program should return an error.
32-
33-
## Step Two
34-
35-
Update your program to recognize multi-character binary strings, replacing garbled numbers with ?
36-
37-
## Step Three
38-
39-
Update your program to recognize all numbers 0 through 9, both individually and as part of a larger string.
40-
41-
```text
42-
_
43-
_|
44-
|_
45-
46-
```
47-
48-
Is converted to "2"
19+
The following input (without the comments) is converted to `"1234567890"`.
4920

5021
```text
5122
_ _ _ _ _ _ _ _ #
52-
| _| _||_||_ |_ ||_||_|| | # decimal numbers.
23+
| _| _||_||_ |_ ||_||_|| | # Decimal numbers.
5324
||_ _| | _||_| ||_| _||_| #
54-
# fourth line is always blank
25+
# The fourth line is always blank,
5526
```
5627

57-
Is converted to "1234567890"
58-
59-
## Step Four
28+
The following input is converted to `"123,456,789"`.
6029

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

6432
```text
65-
_ _
33+
_ _
6634
| _| _|
6735
||_ _|
68-
69-
_ _
70-
|_||_ |_
36+
37+
_ _
38+
|_||_ |_
7139
| _||_|
72-
73-
_ _ _
40+
41+
_ _ _
7442
||_||_|
7543
||_| _|
76-
44+
7745
```
7846

79-
Is converted to "123,456,789".
47+
<!-- prettier-ignore-end -->

exercises/practice/ocr-numbers/.meta/config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"arueckauer",
77
"G-Rath",
88
"kytrinyx",
9-
"petemcfarlane"
9+
"petemcfarlane",
10+
"mk-mxp"
1011
],
1112
"files": {
1213
"solution": [
@@ -21,5 +22,5 @@
2122
},
2223
"blurb": "Given a 3 x 4 grid of pipes, underscores, and spaces, determine which number is represented, or whether it is garbled.",
2324
"source": "Inspired by the Bank OCR kata",
24-
"source_url": "https://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR"
25+
"source_url": "https://codingdojo.org/kata/BankOCR/"
2526
}

exercises/practice/ocr-numbers/.meta/example.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
function recognize($ocr)

exercises/practice/ocr-numbers/OcrNumbersTest.php

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

5+
use PHPUnit\Framework\Attributes\TestDox;
276
use PHPUnit\Framework\TestCase;
287

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

3615
/**
37-
* Recognition result should be returned as a string
16+
* uuid: 5ee54e1a-b554-4bf3-a056-9a7976c3f7e8
3817
*/
39-
18+
#[TestDox('Recognizes 0')]
4019
public function testRecognizes0(): void
4120
{
4221
$input = [
@@ -48,6 +27,10 @@ public function testRecognizes0(): void
4827
$this->assertSame('0', recognize($input));
4928
}
5029

30+
/**
31+
* uuid: 027ada25-17fd-4d78-aee6-35a19623639d
32+
*/
33+
#[TestDox('Recognizes 1')]
5134
public function testRecognizes1(): void
5235
{
5336
$input = [
@@ -60,9 +43,10 @@ public function testRecognizes1(): void
6043
}
6144

6245
/**
63-
* Unreadable but correctly sized inputs return ?
46+
* uuid: 3cce2dbd-01d9-4f94-8fae-419a822e89bb
6447
*/
65-
public function testUnreadable(): void
48+
#[TestDox('Unreadable but correctly sized inputs return ?')]
49+
public function testUnreadableButCorrectlySizedInputsReturnQuestionmark(): void
6650
{
6751
$input = [
6852
" ",
@@ -74,9 +58,10 @@ public function testUnreadable(): void
7458
}
7559

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

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

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

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

93+
/**
94+
* uuid: 4a841794-73c9-4da9-a779-1f9837faff66
95+
*/
96+
#[TestDox('Recognizes 110101100')]
10797
public function testRecognizes110101100(): void
10898
{
10999
$input = [
@@ -116,9 +106,10 @@ public function testRecognizes110101100(): void
116106
}
117107

118108
/**
119-
* Garbled numbers in a string are replaced with ?
109+
* uuid: 70c338f9-85b1-4296-a3a8-122901cdfde8
120110
*/
121-
public function testGarbled(): void
111+
#[TestDox('Garbled numbers in a string are replaced with ?')]
112+
public function testGarbledNumbersInAStringAreReplacedWithQuestionmark(): void
122113
{
123114
$input = [
124115
" _ _ _ ",
@@ -129,6 +120,10 @@ public function testGarbled(): void
129120
$this->assertSame('11?10?1?0', recognize($input));
130121
}
131122

123+
/**
124+
* uuid: ea494ff4-3610-44d7-ab7e-72fdef0e0802
125+
*/
126+
#[TestDox('Recognizes 2')]
132127
public function testRecognizes2(): void
133128
{
134129
$input = [
@@ -140,6 +135,10 @@ public function testRecognizes2(): void
140135
$this->assertSame('2', recognize($input));
141136
}
142137

138+
/**
139+
* uuid: 1acd2c00-412b-4268-93c2-bd7ff8e05a2c
140+
*/
141+
#[TestDox('Recognizes 3')]
143142
public function testRecognizes3(): void
144143
{
145144
$input = [
@@ -151,6 +150,10 @@ public function testRecognizes3(): void
151150
$this->assertSame('3', recognize($input));
152151
}
153152

153+
/**
154+
* uuid: eaec6a15-be17-4b6d-b895-596fae5d1329
155+
*/
156+
#[TestDox('Recognizes 4')]
154157
public function testRecognizes4(): void
155158
{
156159
$input = [
@@ -162,6 +165,10 @@ public function testRecognizes4(): void
162165
$this->assertSame('4', recognize($input));
163166
}
164167

168+
/**
169+
* uuid: 440f397a-f046-4243-a6ca-81ab5406c56e
170+
*/
171+
#[TestDox('Recognizes 5')]
165172
public function testRecognizes5(): void
166173
{
167174
$input = [
@@ -173,6 +180,10 @@ public function testRecognizes5(): void
173180
$this->assertSame('5', recognize($input));
174181
}
175182

183+
/**
184+
* uuid: f4c9cf6a-f1e2-4878-bfc3-9b85b657caa0
185+
*/
186+
#[TestDox('Recognizes 6')]
176187
public function testRecognizes6(): void
177188
{
178189
$input = [
@@ -184,6 +195,10 @@ public function testRecognizes6(): void
184195
$this->assertSame('6', recognize($input));
185196
}
186197

198+
/**
199+
* uuid: e24ebf80-c611-41bb-a25a-ac2c0f232df5
200+
*/
201+
#[TestDox('Recognizes 7')]
187202
public function testRecognizes7(): void
188203
{
189204
$input = [
@@ -195,6 +210,10 @@ public function testRecognizes7(): void
195210
$this->assertSame('7', recognize($input));
196211
}
197212

213+
/**
214+
* uuid: b79cad4f-e264-4818-9d9e-77766792e233
215+
*/
216+
#[TestDox('Recognizes 8')]
198217
public function testRecognizes8(): void
199218
{
200219
$input = [
@@ -206,6 +225,10 @@ public function testRecognizes8(): void
206225
$this->assertSame('8', recognize($input));
207226
}
208227

228+
/**
229+
* uuid: 5efc9cfc-9227-4688-b77d-845049299e66
230+
*/
231+
#[TestDox('Recognizes 9')]
209232
public function testRecognizes9(): void
210233
{
211234
$input = [
@@ -217,6 +240,10 @@ public function testRecognizes9(): void
217240
$this->assertSame('9', recognize($input));
218241
}
219242

243+
/**
244+
* uuid: f60cb04a-42be-494e-a535-3451c8e097a4
245+
*/
246+
#[TestDox('Recognizes string of decimal numbers')]
220247
public function testRecognizesStringOfDecimalNumbers(): void
221248
{
222249
$input = [
@@ -229,9 +256,10 @@ public function testRecognizesStringOfDecimalNumbers(): void
229256
}
230257

231258
/**
232-
* Numbers separated by empty lines are recognized. Lines are joined by commas.
259+
* uuid: b73ecf8b-4423-4b36-860d-3710bdb8a491
233260
*/
234-
public function testLinesWithCommas(): void
261+
#[TestDox('Numbers separated by empty lines are recognized. Lines are joined by commas.')]
262+
public function testNumbersSeparatedByEmptyLinesAreRecognizedLinesAreJoinedByCommas(): void
235263
{
236264
$input = [
237265
" _ _ ",

0 commit comments

Comments
 (0)