-
-
Notifications
You must be signed in to change notification settings - Fork 34
[ 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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
} |
19 changes: 19 additions & 0 deletions
19
exercises/practice/ocr-numbers/.meta/solutions/lib/OcrNumbers.rakumod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
exercises/practice/ocr-numbers/.meta/solutions/t/ocr-numbers.rakutest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../t/ocr-numbers.rakutest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
unit module OcrNumbers; | ||
|
||
sub ascii-to-digits ( $art ) is export { | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.