Skip to content
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
22 changes: 15 additions & 7 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@
"prerequisites": [],
"difficulty": 8
},
{
"slug": "ocr-numbers",
"name": "OCR Numbers",
"uuid": "46c8fd20-887f-4cd6-ac3a-9717d169de1d",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "pascals-triangle",
"name": "Pascal's Triangle",
Expand Down Expand Up @@ -441,19 +449,19 @@
"practices": [],
"prerequisites": [],
"difficulty": 8
},
},
{
"slug": "saddle-points",
"name": "Saddle Points",
"uuid": "d050e56b-9f86-416c-baa3-10a98f47b944",
"slug": "run-length-encoding",
"name": "Run-Length Encoding",
"uuid": "065feae7-f6fc-46b9-a226-d3d9df9d192b",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "run-length-encoding",
"name": "Run-Length Encoding",
"uuid": "065feae7-f6fc-46b9-a226-d3d9df9d192b",
"slug": "saddle-points",
"name": "Saddle Points",
"uuid": "d050e56b-9f86-416c-baa3-10a98f47b944",
"practices": [],
"prerequisites": [],
"difficulty": 8
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": [
"jimmytty"
],
"files": {
"solution": [
"ocr-numbers.sql"
],
"test": [
"ocr-numbers_test.sql"
],
"example": [
".meta/example.sql"
]
},
"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/"
}
130 changes: 130 additions & 0 deletions exercises/practice/ocr-numbers/.meta/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
UPDATE "ocr-numbers"
SET error = 'Number of input lines is not a multiple of four'
WHERE (LENGTH(input) - LENGTH(REPLACE(input, CHAR(10), '')) + 1) % 4 != 0
;
UPDATE "ocr-numbers"
SET error = 'Number of input columns is not a multiple of three'
WHERE LENGTH(REPLACE(input, CHAR(10), '')) % 3 != 0
;


DROP TABLE IF EXISTS dict;
CREATE TEMPORARY TABLE dict AS
WITH RECURSIVE rcte (string, idx, font, digit) AS (
VALUES (
(WITH cte (fonts) AS (
VALUES
(' _ _ _ _ _ _ _ _ '),
(' | _| _||_||_ |_ ||_||_|| |'),
(' ||_ _| | _||_| ||_| _||_|'),
(' '))
SELECT GROUP_CONCAT(fonts, CHAR(10)) FROM cte),
1, NULL, 0
)
UNION ALL
SELECT string,
idx + 3,
REPLACE(
PRINTF('%s\n%s\n%s\n%s',
SUBSTR(string, INSTR(string, CHAR(10)) * 0 + idx, 3),
SUBSTR(string, INSTR(string, CHAR(10)) * 1 + idx, 3),
SUBSTR(string, INSTR(string, CHAR(10)) * 2 + idx, 3),
SUBSTR(string, INSTR(string, CHAR(10)) * 3 + idx, 3)
),
'\n',
CHAR(10)
),
IIF(digit + 1 = 10, 0, digit + 1)
FROM rcte
WHERE idx < 30
) SELECT font, digit FROM rcte WHERE font NOTNULL;

UPDATE "ocr-numbers"
SET result = (
WITH
split_lines AS (
WITH RECURSIVE rcte (string, idx, line) AS (
VALUES (input, 0, NULL)
UNION ALL
SELECT string,
idx + 4,
LTRIM(
PRINTF(
'%s%s%s%s',
SUBSTR(
string,
INSTR(string, char(10)) * (idx + 0),
INSTR(string, char(10))
),
SUBSTR(
string,
INSTR(string, char(10)) * (idx + 1),
INSTR(string, char(10))
),
SUBSTR(
string,
INSTR(string, char(10)) * (idx + 2),
INSTR(string, char(10))
),
SUBSTR(
string,
INSTR(string, char(10)) * (idx+3),
INSTR(string, char(10))
)
),
CHAR(10)
)
FROM rcte
WHERE idx <=
LENGTH(string) - LENGTH(replace(string, char(10),''))
) SELECT line FROM rcte WHERE line NOTNULL
),
converter AS (
SELECT line,
(WITH RECURSIVE split_numbers (string, idx, font) AS (
VALUES (line, 1, NULL)
UNION ALL
SELECT string,
idx + 3,
REPLACE(
PRINTF(
'%s\n%s\n%s\n%s',
SUBSTR(
string,
INSTR(string, CHAR(10)) * 0 + idx, 3
),
SUBSTR(
string,
INSTR(string, CHAR(10)) * 1 + idx, 3
),
SUBSTR(
string,
INSTR(string, CHAR(10)) * 2 + idx, 3
),
SUBSTR(
string,
INSTR(string, CHAR(10)) * 3 + idx, 3
)
),
'\n',
CHAR(10)
)
FROM split_numbers
WHERE idx < (SELECT INSTR(string, CHAR(10)))
)
SELECT GROUP_CONCAT(digit, '') AS number
FROM (
SELECT COALESCE(
(SELECT digit
FROM dict
WHERE dict.font = split_numbers.font),
'?'
) AS digit
FROM split_numbers WHERE font NOTNULL
)
) AS number
FROM split_lines
) SELECT GROUP_CONCAT(number) AS number FROM converter
)
WHERE error ISNULL
;
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."
12 changes: 12 additions & 0 deletions exercises/practice/ocr-numbers/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DROP TABLE IF EXISTS "ocr-numbers";
CREATE TABLE "ocr-numbers" (
input TEXT NOT NULL,
result TEXT ,
error TEXT
);

.mode csv
.import ./data.csv "ocr-numbers"

UPDATE "ocr-numbers" SET result = NULL, error = NULL;
UPDATE "ocr-numbers" SET input = REPLACE(input, '\n', CHAR(10));
38 changes: 38 additions & 0 deletions exercises/practice/ocr-numbers/create_test_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
DROP TABLE IF EXISTS tests;
CREATE TABLE IF NOT EXISTS tests (
-- uuid and description are taken from the test.toml file
uuid TEXT PRIMARY KEY,
description TEXT NOT NULL,
-- The following section is needed by the online test-runner
status TEXT DEFAULT 'fail',
message TEXT,
output TEXT,
test_code TEXT,
task_id INTEGER DEFAULT NULL,
-- Here are columns for the actual tests
input TEXT NOT NULL,
expected_result TEXT,
expected_error TEXT
);

INSERT INTO tests (uuid, description, input, expected_result, expected_error)
VALUES
('5ee54e1a-b554-4bf3-a056-9a7976c3f7e8', 'Recognizes 0', ' _ \n| |\n|_|\n ', '0', NULL),
('027ada25-17fd-4d78-aee6-35a19623639d', 'Recognizes 1', ' \n |\n |\n ', '1', NULL),
('3cce2dbd-01d9-4f94-8fae-419a822e89bb', 'Unreadable but correctly sized inputs return ?', ' \n _\n |\n ', '?', NULL),
('cb19b733-4e36-4cf9-a4a1-6e6aac808b9a', 'Input with a number of lines that is not a multiple of four raises an error', ' _ \n| |\n ', NULL, 'Number of input lines is not a multiple of four'),
('235f7bd1-991b-4587-98d4-84206eec4cc6', 'Input with a number of columns that is not a multiple of three raises an error', ' \n |\n |\n ', NULL, 'Number of input columns is not a multiple of three'),
('4a841794-73c9-4da9-a779-1f9837faff66', 'Recognizes 110101100', ' _ _ _ _ \n | || | || | | || || |\n | ||_| ||_| | ||_||_|\n ', '110101100', NULL),
('70c338f9-85b1-4296-a3a8-122901cdfde8', 'Garbled numbers in a string are replaced with ?', ' _ _ _ \n | || | || | || || |\n | | _| ||_| | ||_||_|\n ', '11?10?1?0', NULL),
('ea494ff4-3610-44d7-ab7e-72fdef0e0802', 'Recognizes 2', ' _ \n _|\n|_ \n ', '2', NULL),
('1acd2c00-412b-4268-93c2-bd7ff8e05a2c', 'Recognizes 3', ' _ \n _|\n _|\n ', '3', NULL),
('eaec6a15-be17-4b6d-b895-596fae5d1329', 'Recognizes 4', ' \n|_|\n |\n ', '4', NULL),
('440f397a-f046-4243-a6ca-81ab5406c56e', 'Recognizes 5', ' _ \n|_ \n _|\n ', '5', NULL),
('f4c9cf6a-f1e2-4878-bfc3-9b85b657caa0', 'Recognizes 6', ' _ \n|_ \n|_|\n ', '6', NULL),
('e24ebf80-c611-41bb-a25a-ac2c0f232df5', 'Recognizes 7', ' _ \n |\n |\n ', '7', NULL),
('b79cad4f-e264-4818-9d9e-77766792e233', 'Recognizes 8', ' _ \n|_|\n|_|\n ', '8', NULL),
('5efc9cfc-9227-4688-b77d-845049299e66', 'Recognizes 9', ' _ \n|_|\n _|\n ', '9', NULL),
('f60cb04a-42be-494e-a535-3451c8e097a4', 'Recognizes string of decimal numbers', ' _ _ _ _ _ _ _ _ \n | _| _||_||_ |_ ||_||_|| |\n ||_ _| | _||_| ||_| _||_|\n ', '1234567890', NULL),
('b73ecf8b-4423-4b36-860d-3710bdb8a491', 'Numbers separated by empty lines are recognized. Lines are joined by commas.', ' _ _ \n | _| _|\n ||_ _|\n \n _ _ \n|_||_ |_ \n | _||_|\n \n _ _ _ \n ||_||_|\n ||_| _|\n ', '123,456,789', NULL);

UPDATE tests SET input = REPLACE(input, '\n', CHAR(10));
Loading