Skip to content

Commit c2589db

Browse files
jimmyttyIsaacG
andauthored
Add practice exercise: proverb (#124)
* * add practice exercise: proverb * * add instructions.append.md * Update exercises/practice/proverb/.docs/instructions.append.md Co-authored-by: Isaac Good <[email protected]> * Update exercises/practice/proverb/create_fixture.sql Co-authored-by: Isaac Good <[email protected]> * Update exercises/practice/proverb/proverb.sql Co-authored-by: Isaac Good <[email protected]> * * removing duplicate info from instructions.append.md * Update exercises/practice/proverb/proverb.sql Co-authored-by: Isaac Good <[email protected]> --------- Co-authored-by: Isaac Good <[email protected]>
1 parent 80a3e2b commit c2589db

File tree

11 files changed

+203
-0
lines changed

11 files changed

+203
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,14 @@
402402
"prerequisites": [],
403403
"difficulty": 8
404404
},
405+
{
406+
"slug": "proverb",
407+
"name": "Proverb",
408+
"uuid": "10c51241-2399-4a0c-84cf-390a000f3c3b",
409+
"practices": [],
410+
"prerequisites": [],
411+
"difficulty": 8
412+
},
405413
{
406414
"slug": "rest-api",
407415
"name": "REST API",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SQLite-specific instructions
2+
3+
## JSON documentation
4+
5+
[JSON Functions And Operators][json-docs]
6+
7+
[json-docs]: https://www.sqlite.org/json1.html
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Instructions
2+
3+
For want of a horseshoe nail, a kingdom was lost, or so the saying goes.
4+
5+
Given a list of inputs, generate the relevant proverb.
6+
For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme:
7+
8+
```text
9+
For want of a nail the shoe was lost.
10+
For want of a shoe the horse was lost.
11+
For want of a horse the rider was lost.
12+
For want of a rider the message was lost.
13+
For want of a message the battle was lost.
14+
For want of a battle the kingdom was lost.
15+
And all for the want of a nail.
16+
```
17+
18+
Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content.
19+
No line of the output text should be a static, unchanging string; all should vary according to the input given.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"proverb.sql"
8+
],
9+
"test": [
10+
"proverb_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
14+
]
15+
},
16+
"blurb": "For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output the full text of this proverbial rhyme.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/For_Want_of_a_Nail"
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
UPDATE proverb
2+
SET result = ''
3+
WHERE JSON(strings) = '[]'
4+
;
5+
6+
UPDATE proverb
7+
SET result = (
8+
WITH RECURSIVE to_pieces (strings, i, len, piece) AS (
9+
VALUES(strings, 0, JSON_ARRAY_LENGTH(strings) - 1, NULL)
10+
UNION ALL
11+
SELECT strings, i+1, len,
12+
PRINTF(
13+
'For want of a %s the %s was lost.',
14+
JSON_EXTRACT(strings, PRINTF('$[%d]', i)),
15+
JSON_EXTRACT(strings, PRINTF('$[%d]', i+1))
16+
)
17+
FROM to_pieces
18+
WHERE i < len
19+
)
20+
SELECT group_concat(piece, CHAR(10))
21+
FROM (
22+
SELECT piece
23+
FROM to_pieces
24+
WHERE piece NOTNULL
25+
UNION ALL
26+
SELECT PRINTF(
27+
'And all for the want of a %s.',
28+
JSON_EXTRACT(strings, '$[0]')
29+
)
30+
)
31+
)
32+
WHERE result ISNULL
33+
;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
[e974b73e-7851-484f-8d6d-92e07fe742fc]
13+
description = "zero pieces"
14+
15+
[2fcd5f5e-8b82-4e74-b51d-df28a5e0faa4]
16+
description = "one piece"
17+
18+
[d9d0a8a1-d933-46e2-aa94-eecf679f4b0e]
19+
description = "two pieces"
20+
21+
[c95ef757-5e94-4f0d-a6cb-d2083f5e5a83]
22+
description = "three pieces"
23+
24+
[433fb91c-35a2-4d41-aeab-4de1e82b2126]
25+
description = "full proverb"
26+
27+
[c1eefa5a-e8d9-41c7-91d4-99fab6d6b9f7]
28+
description = "four pieces modernized"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DROP TABLE IF EXISTS proverb;
2+
CREATE TABLE proverb (
3+
strings TEXT NOT NULL, -- json array containing the input words
4+
result TEXT
5+
);
6+
7+
.mode csv
8+
.import ./data.csv proverb
9+
10+
UPDATE proverb SET result = NULL;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
DROP TABLE IF EXISTS tests;
2+
CREATE TABLE IF NOT EXISTS tests (
3+
-- uuid and description are taken from the test.toml file
4+
uuid TEXT PRIMARY KEY,
5+
description TEXT NOT NULL,
6+
-- The following section is needed by the online test-runner
7+
status TEXT DEFAULT 'fail',
8+
message TEXT,
9+
output TEXT,
10+
test_code TEXT,
11+
task_id INTEGER DEFAULT NULL,
12+
-- Here are columns for the actual tests
13+
strings TEXT NOT NULL, -- json array
14+
expected TEXT NOT NULL
15+
);
16+
17+
INSERT INTO tests (uuid, description, strings, expected)
18+
VALUES
19+
('e974b73e-7851-484f-8d6d-92e07fe742fc', 'zero pieces', '[]', ''),
20+
('2fcd5f5e-8b82-4e74-b51d-df28a5e0faa4', 'one piece', '["nail"]', 'And all for the want of a nail.'),
21+
('d9d0a8a1-d933-46e2-aa94-eecf679f4b0e', 'two pieces', '["nail","shoe"]', 'For want of a nail the shoe was lost.\nAnd all for the want of a nail.'),
22+
('c95ef757-5e94-4f0d-a6cb-d2083f5e5a83', 'three pieces', '["nail","shoe","horse"]', 'For want of a nail the shoe was lost.\nFor want of a shoe the horse was lost.\nAnd all for the want of a nail.'),
23+
('433fb91c-35a2-4d41-aeab-4de1e82b2126', 'full proverb', '["nail","shoe","horse","rider","message","battle","kingdom"]', 'For want of a nail the shoe was lost.\nFor want of a shoe the horse was lost.\nFor want of a horse the rider was lost.\nFor want of a rider the message was lost.\nFor want of a message the battle was lost.\nFor want of a battle the kingdom was lost.\nAnd all for the want of a nail.'),
24+
('c1eefa5a-e8d9-41c7-91d4-99fab6d6b9f7', 'four pieces modernized', '["pin","gun","soldier","battle"]', 'For want of a pin the gun was lost.\nFor want of a gun the soldier was lost.\nFor want of a soldier the battle was lost.\nAnd all for the want of a pin.');
25+
26+
UPDATE tests SET expected = REPLACE(expected, '\n', CHAR(10));

exercises/practice/proverb/data.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"[]",
2+
"[""nail""]",
3+
"[""nail"",""shoe""]",
4+
"[""nail"",""shoe"",""horse""]",
5+
"[""nail"",""shoe"",""horse"",""rider"",""message"",""battle"",""kingdom""]",
6+
"[""pin"",""gun"",""soldier"",""battle""]",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Schema:
2+
-- CREATE TABLE proverb (
3+
-- strings TEXT NOT NULL, -- json array containing the input words
4+
-- result TEXT
5+
-- );
6+
--
7+
-- Task: update proverb table and set result column based on the strings.

0 commit comments

Comments
 (0)