Skip to content

[ Implementation ] OCR Numbers #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2025
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "ocr-numbers",
"name": "OCR Numbers",
"uuid": "00f7b37c-b460-47ed-949e-d62469d8d6d5",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
},
Expand Down
79 changes: 79 additions & 0 deletions exercises/practice/ocr-numbers/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Instructions

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.

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

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"

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

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

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

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

```

Is converted to "123,456,789".
19 changes: 19 additions & 0 deletions exercises/practice/ocr-numbers/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"habere-et-dispertire"
],
"files": {
"solution": [
"lib/OcrNumbers.rakumod"
],
"test": [
"t/ocr-numbers.rakutest"
],
"example": [
".meta/solutions/lib/OcrNumbers.rakumod"
]
},
"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/kata/BankOCR/"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
unit module OcrNumbers;

sub ascii-to-digits ( $art where { .lines.elems %% 4 and .lines.map( *.chars ).all %% 3 } ) is export {
constant $NUMBERS = q:to/0123456789/;
_ _ _ _ _ _ _ _
| | | _| _||_||_ |_ ||_||_|
|_| ||_ _| | _||_| ||_| _|

0123456789
constant %LOOKUP = ^10 RZ=> ( map *.join( "\n" ), zip $NUMBERS.lines.map( *.comb ).map: *.rotor: 3 );

join ',', gather for $art.lines.rotor: 4 {
take join '',
map { .defined ?? $_ !! '?' },
%LOOKUP{
map *.join( "\n" ), zip .map( *.comb ).map: *.rotor: 3
}
}
}
50 changes: 50 additions & 0 deletions exercises/practice/ocr-numbers/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
properties:
convert:
test: |-
if %case<expected><error>:exists {
sprintf(q :to/END/, "\n " ~ %case<input><rows>.join("\n ").Str, %case<description>.raku);
dies-ok( {
ascii-to-digits(q:to/SOUPÇON/)%s
SOUPÇON
},
%s,
);
END
}
else {
sprintf(q :to/END/, "\n " ~ %case<input><rows>.join("\n ").Str, %case<expected>.Str, %case<description>.raku);
cmp-ok(
ascii-to-digits(q:to/SOUPÇON/)%s
SOUPÇON
,
'eq',
'%s',
%s,
);
END
}

unit: module
example: |-
sub ascii-to-digits ( $art where { .lines.elems %% 4 and .lines.map( *.chars ).all %% 3 } ) is export {
constant $NUMBERS = q:to/0123456789/;
_ _ _ _ _ _ _ _
| | | _| _||_||_ |_ ||_||_|
|_| ||_ _| | _||_| ||_| _|

0123456789
constant %LOOKUP = ^10 RZ=> ( map *.join( "\n" ), zip $NUMBERS.lines.map( *.comb ).map: *.rotor: 3 );

join ',', gather for $art.lines.rotor: 4 {
take join '',
map { .defined ?? $_ !! '?' },
%LOOKUP{
map *.join( "\n" ), zip .map( *.comb ).map: *.rotor: 3
}
}
}

stub: |-
sub ascii-to-digits ( $art ) is export {
}

61 changes: 61 additions & 0 deletions exercises/practice/ocr-numbers/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[5ee54e1a-b554-4bf3-a056-9a7976c3f7e8]
description = "Recognizes 0"

[027ada25-17fd-4d78-aee6-35a19623639d]
description = "Recognizes 1"

[3cce2dbd-01d9-4f94-8fae-419a822e89bb]
description = "Unreadable but correctly sized inputs return ?"

[cb19b733-4e36-4cf9-a4a1-6e6aac808b9a]
description = "Input with a number of lines that is not a multiple of four raises an error"

[235f7bd1-991b-4587-98d4-84206eec4cc6]
description = "Input with a number of columns that is not a multiple of three raises an error"

[4a841794-73c9-4da9-a779-1f9837faff66]
description = "Recognizes 110101100"

[70c338f9-85b1-4296-a3a8-122901cdfde8]
description = "Garbled numbers in a string are replaced with ?"

[ea494ff4-3610-44d7-ab7e-72fdef0e0802]
description = "Recognizes 2"

[1acd2c00-412b-4268-93c2-bd7ff8e05a2c]
description = "Recognizes 3"

[eaec6a15-be17-4b6d-b895-596fae5d1329]
description = "Recognizes 4"

[440f397a-f046-4243-a6ca-81ab5406c56e]
description = "Recognizes 5"

[f4c9cf6a-f1e2-4878-bfc3-9b85b657caa0]
description = "Recognizes 6"

[e24ebf80-c611-41bb-a25a-ac2c0f232df5]
description = "Recognizes 7"

[b79cad4f-e264-4818-9d9e-77766792e233]
description = "Recognizes 8"

[5efc9cfc-9227-4688-b77d-845049299e66]
description = "Recognizes 9"

[f60cb04a-42be-494e-a535-3451c8e097a4]
description = "Recognizes string of decimal numbers"

[b73ecf8b-4423-4b36-860d-3710bdb8a491]
description = "Numbers separated by empty lines are recognized. Lines are joined by commas."
4 changes: 4 additions & 0 deletions exercises/practice/ocr-numbers/lib/OcrNumbers.rakumod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
unit module OcrNumbers;

sub ascii-to-digits ( $art ) is export {
}
Loading