Skip to content

Commit 61451e7

Browse files
Add ocr-numbers exercise (#271)
1 parent 7eff5a5 commit 61451e7

File tree

7 files changed

+409
-0
lines changed

7 files changed

+409
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,14 @@
728728
"prerequisites": [],
729729
"difficulty": 5
730730
},
731+
{
732+
"slug": "ocr-numbers",
733+
"name": "OCR Numbers",
734+
"uuid": "5890a278-557d-443f-8bbf-8abed66b185e",
735+
"practices": [],
736+
"prerequisites": [],
737+
"difficulty": 5
738+
},
731739
{
732740
"slug": "parallel-letter-frequency",
733741
"name": "Parallel Letter Frequency",
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Instructions
2+
3+
Given a 3 x 4 grid of pipes, underscores, and spaces, determine which number is represented, or whether it is garbled.
4+
5+
## Step One
6+
7+
To begin with, convert a simple binary font to a string containing 0 or 1.
8+
9+
The binary font uses pipes and underscores, four rows high and three columns wide.
10+
11+
```text
12+
_ #
13+
| | # zero.
14+
|_| #
15+
# the fourth row is always blank
16+
```
17+
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"
49+
50+
```text
51+
_ _ _ _ _ _ _ _ #
52+
| _| _||_||_ |_ ||_||_|| | # decimal numbers.
53+
||_ _| | _||_| ||_| _||_| #
54+
# fourth line is always blank
55+
```
56+
57+
Is converted to "1234567890"
58+
59+
## Step Four
60+
61+
Update your program to handle multiple numbers, one per line.
62+
When converting several lines, join the lines with commas.
63+
64+
```text
65+
_ _
66+
| _| _|
67+
||_ _|
68+
69+
_ _
70+
|_||_ |_
71+
| _||_|
72+
73+
_ _ _
74+
||_||_|
75+
||_| _|
76+
77+
```
78+
79+
Is converted to "123,456,789".
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+
"ocr-numbers.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module main
2+
3+
import strings
4+
5+
fn convert_digit(first string, second string, third string, fourth string) rune {
6+
return if fourth != ' ' {
7+
`?`
8+
} else if first == ' _ ' && second == '| |' && third == '|_|' {
9+
`0`
10+
} else if first == ' ' && second == ' |' && third == ' |' {
11+
`1`
12+
} else if first == ' _ ' && second == ' _|' && third == '|_ ' {
13+
`2`
14+
} else if first == ' _ ' && second == ' _|' && third == ' _|' {
15+
`3`
16+
} else if first == ' ' && second == '|_|' && third == ' |' {
17+
`4`
18+
} else if first == ' _ ' && second == '|_ ' && third == ' _|' {
19+
`5`
20+
} else if first == ' _ ' && second == '|_ ' && third == '|_|' {
21+
`6`
22+
} else if first == ' _ ' && second == ' |' && third == ' |' {
23+
`7`
24+
} else if first == ' _ ' && second == '|_|' && third == '|_|' {
25+
`8`
26+
} else if first == ' _ ' && second == '|_|' && third == ' _|' {
27+
`9`
28+
} else {
29+
`?`
30+
}
31+
}
32+
33+
fn convert(rows []string) !string {
34+
if rows.len % 4 != 0 {
35+
return error('Number of input lines is not a multiple of four')
36+
}
37+
if rows[0].len % 3 != 0 {
38+
return error('Number of input columns is not a multiple of three')
39+
}
40+
r := rows.len / 4
41+
c := rows[0].len / 3
42+
mut builder := strings.new_builder(r * (c + 1) - 1)
43+
for i in 0 .. r {
44+
for j in 0 .. c {
45+
top := 4 * i
46+
left := 3 * j
47+
right := 3 * (j + 1)
48+
first := rows[top][left..right]
49+
second := rows[top + 1][left..right]
50+
third := rows[top + 2][left..right]
51+
fourth := rows[top + 3][left..right]
52+
builder.write_rune(convert_digit(first, second, third, fourth))
53+
}
54+
if i + 1 < r {
55+
builder.write_rune(`,`)
56+
}
57+
}
58+
return builder.str()
59+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[5ee54e1a-b554-4bf3-a056-9a7976c3f7e8]
6+
description = "Recognizes 0"
7+
8+
[027ada25-17fd-4d78-aee6-35a19623639d]
9+
description = "Recognizes 1"
10+
11+
[3cce2dbd-01d9-4f94-8fae-419a822e89bb]
12+
description = "Unreadable but correctly sized inputs return ?"
13+
14+
[cb19b733-4e36-4cf9-a4a1-6e6aac808b9a]
15+
description = "Input with a number of lines that is not a multiple of four raises an error"
16+
17+
[235f7bd1-991b-4587-98d4-84206eec4cc6]
18+
description = "Input with a number of columns that is not a multiple of three raises an error"
19+
20+
[4a841794-73c9-4da9-a779-1f9837faff66]
21+
description = "Recognizes 110101100"
22+
23+
[70c338f9-85b1-4296-a3a8-122901cdfde8]
24+
description = "Garbled numbers in a string are replaced with ?"
25+
26+
[ea494ff4-3610-44d7-ab7e-72fdef0e0802]
27+
description = "Recognizes 2"
28+
29+
[1acd2c00-412b-4268-93c2-bd7ff8e05a2c]
30+
description = "Recognizes 3"
31+
32+
[eaec6a15-be17-4b6d-b895-596fae5d1329]
33+
description = "Recognizes 4"
34+
35+
[440f397a-f046-4243-a6ca-81ab5406c56e]
36+
description = "Recognizes 5"
37+
38+
[f4c9cf6a-f1e2-4878-bfc3-9b85b657caa0]
39+
description = "Recognizes 6"
40+
41+
[e24ebf80-c611-41bb-a25a-ac2c0f232df5]
42+
description = "Recognizes 7"
43+
44+
[b79cad4f-e264-4818-9d9e-77766792e233]
45+
description = "Recognizes 8"
46+
47+
[5efc9cfc-9227-4688-b77d-845049299e66]
48+
description = "Recognizes 9"
49+
50+
[f60cb04a-42be-494e-a535-3451c8e097a4]
51+
description = "Recognizes string of decimal numbers"
52+
53+
[b73ecf8b-4423-4b36-860d-3710bdb8a491]
54+
description = "Numbers separated by empty lines are recognized. Lines are joined by commas."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module main
2+
3+
fn convert(rows []string) !string {
4+
}

0 commit comments

Comments
 (0)