Skip to content

Commit 6327aa8

Browse files
Add ocr-numbers exercise (#353)
1 parent bc8fbf6 commit 6327aa8

8 files changed

Lines changed: 405 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,14 @@
717717
"prerequisites": [],
718718
"difficulty": 5
719719
},
720+
{
721+
"slug": "ocr-numbers",
722+
"name": "OCR Numbers",
723+
"uuid": "ab3b1595-ebb0-46ed-ae5c-46e4c254c07e",
724+
"practices": [],
725+
"prerequisites": [],
726+
"difficulty": 5
727+
},
720728
{
721729
"slug": "pig-latin",
722730
"name": "Pig Latin",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Instructions
2+
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 `","`.
6+
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 (`" "`).
11+
12+
## Edge cases
13+
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.
16+
17+
## Examples
18+
19+
The following input (without the comments) is converted to `"1234567890"`.
20+
21+
```text
22+
_ _ _ _ _ _ _ _ #
23+
| _| _||_||_ |_ ||_||_|| | # Decimal numbers.
24+
||_ _| | _||_| ||_| _||_| #
25+
# The fourth line is always blank,
26+
```
27+
28+
The following input is converted to `"123,456,789"`.
29+
30+
<!-- prettier-ignore-start -->
31+
32+
```text
33+
_ _
34+
| _| _|
35+
||_ _|
36+
37+
_ _
38+
|_||_ |_
39+
| _||_|
40+
41+
_ _ _
42+
||_||_|
43+
||_| _|
44+
45+
```
46+
47+
<!-- prettier-ignore-end -->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Introduction
2+
3+
Your best friend Marta recently landed their dream job working with a local history museum's collections.
4+
Knowing of your interests in programming, they confide in you about an issue at work for an upcoming exhibit on computing history.
5+
A local university's math department had donated several boxes of historical printouts, but given the poor condition of the documents, the decision has been made to digitize the text.
6+
However, the university's old printer had some quirks in how text was represented, and your friend could use your help to extract the data successfully.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"source/ocr_numbers.d"
8+
],
9+
"test": [
10+
"source/ocr_numbers.d"
11+
],
12+
"example": [
13+
"example/ocr_numbers.d"
14+
]
15+
},
16+
"blurb": "Given a 3 x 4 grid of pipes, underscores, and spaces, determine which number is represented, or whether it is garbled.",
17+
"source": "Inspired by the Bank OCR kata",
18+
"source_url": "https://codingdojo.org/kata/BankOCR/"
19+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[5ee54e1a-b554-4bf3-a056-9a7976c3f7e8]
13+
description = "Recognizes 0"
14+
15+
[027ada25-17fd-4d78-aee6-35a19623639d]
16+
description = "Recognizes 1"
17+
18+
[3cce2dbd-01d9-4f94-8fae-419a822e89bb]
19+
description = "Unreadable but correctly sized inputs return ?"
20+
21+
[cb19b733-4e36-4cf9-a4a1-6e6aac808b9a]
22+
description = "Input with a number of lines that is not a multiple of four raises an error"
23+
24+
[235f7bd1-991b-4587-98d4-84206eec4cc6]
25+
description = "Input with a number of columns that is not a multiple of three raises an error"
26+
27+
[4a841794-73c9-4da9-a779-1f9837faff66]
28+
description = "Recognizes 110101100"
29+
30+
[70c338f9-85b1-4296-a3a8-122901cdfde8]
31+
description = "Garbled numbers in a string are replaced with ?"
32+
33+
[ea494ff4-3610-44d7-ab7e-72fdef0e0802]
34+
description = "Recognizes 2"
35+
36+
[1acd2c00-412b-4268-93c2-bd7ff8e05a2c]
37+
description = "Recognizes 3"
38+
39+
[eaec6a15-be17-4b6d-b895-596fae5d1329]
40+
description = "Recognizes 4"
41+
42+
[440f397a-f046-4243-a6ca-81ab5406c56e]
43+
description = "Recognizes 5"
44+
45+
[f4c9cf6a-f1e2-4878-bfc3-9b85b657caa0]
46+
description = "Recognizes 6"
47+
48+
[e24ebf80-c611-41bb-a25a-ac2c0f232df5]
49+
description = "Recognizes 7"
50+
51+
[b79cad4f-e264-4818-9d9e-77766792e233]
52+
description = "Recognizes 8"
53+
54+
[5efc9cfc-9227-4688-b77d-845049299e66]
55+
description = "Recognizes 9"
56+
57+
[f60cb04a-42be-494e-a535-3451c8e097a4]
58+
description = "Recognizes string of decimal numbers"
59+
60+
[b73ecf8b-4423-4b36-860d-3710bdb8a491]
61+
description = "Numbers separated by empty lines are recognized. Lines are joined by commas."
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name "ocr-numbers"
2+
buildRequirements "disallowDeprecations"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module ocr_numbers;
2+
3+
pure string convert(immutable string[] rows)
4+
{
5+
if (rows.length % 4 != 0)
6+
throw new Exception("Number of input lines is not a multiple of four");
7+
8+
if (rows.length > 0 && rows[0].length % 3 != 0)
9+
throw new Exception("Number of input columns is not a multiple of three");
10+
11+
string[] groups;
12+
for (size_t g = 0; g < rows.length; g += 4)
13+
groups ~= convertGroup(rows[g .. g + 4]);
14+
15+
import std.array : join;
16+
return groups.join(",");
17+
}
18+
19+
private pure string convertGroup(immutable string[] rows)
20+
{
21+
immutable size_t cols = rows[0].length;
22+
char[] result;
23+
for (size_t c = 0; c < cols; c += 3)
24+
result ~= recognize(rows, c);
25+
return cast(string) result;
26+
}
27+
28+
private pure char recognize(immutable string[] rows, size_t col)
29+
{
30+
immutable string[10][4] patterns = [
31+
[" _ ", " ", " _ ", " _ ", " ", " _ ", " _ ", " _ ", " _ ", " _ "],
32+
["| |", " |", " _|", " _|", "|_|", "|_ ", "|_ ", " |", "|_|", "|_|"],
33+
["|_|", " |", "|_ ", " _|", " |", " _|", "|_|", " |", "|_|", " _|"],
34+
[" ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
35+
];
36+
37+
foreach (d; 0 .. 10)
38+
{
39+
bool match = true;
40+
foreach (r; 0 .. 4)
41+
{
42+
if (rows[r][col .. col + 3] != patterns[r][d])
43+
{
44+
match = false;
45+
break;
46+
}
47+
}
48+
if (match)
49+
return cast(char)('0' + d);
50+
}
51+
return '?';
52+
}

0 commit comments

Comments
 (0)