Skip to content

Commit f03eb06

Browse files
authored
Add practice exercise: food-chain (#205)
1 parent aac949a commit f03eb06

File tree

10 files changed

+332
-0
lines changed

10 files changed

+332
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@
370370
"prerequisites": [],
371371
"difficulty": 8
372372
},
373+
{
374+
"slug": "food-chain",
375+
"name": "Food Chain",
376+
"uuid": "128ab281-7720-4383-be90-a2b311bd8c38",
377+
"practices": [],
378+
"prerequisites": [],
379+
"difficulty": 8
380+
},
373381
{
374382
"slug": "hamming",
375383
"name": "Hamming",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Instructions
2+
3+
Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.
4+
5+
While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.
6+
7+
This is a [cumulative song][cumulative-song] of unknown origin.
8+
9+
This is one of many common variants.
10+
11+
```text
12+
I know an old lady who swallowed a fly.
13+
I don't know why she swallowed the fly. Perhaps she'll die.
14+
15+
I know an old lady who swallowed a spider.
16+
It wriggled and jiggled and tickled inside her.
17+
She swallowed the spider to catch the fly.
18+
I don't know why she swallowed the fly. Perhaps she'll die.
19+
20+
I know an old lady who swallowed a bird.
21+
How absurd to swallow a bird!
22+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
23+
She swallowed the spider to catch the fly.
24+
I don't know why she swallowed the fly. Perhaps she'll die.
25+
26+
I know an old lady who swallowed a cat.
27+
Imagine that, to swallow a cat!
28+
She swallowed the cat to catch the bird.
29+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
30+
She swallowed the spider to catch the fly.
31+
I don't know why she swallowed the fly. Perhaps she'll die.
32+
33+
I know an old lady who swallowed a dog.
34+
What a hog, to swallow a dog!
35+
She swallowed the dog to catch the cat.
36+
She swallowed the cat to catch the bird.
37+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
38+
She swallowed the spider to catch the fly.
39+
I don't know why she swallowed the fly. Perhaps she'll die.
40+
41+
I know an old lady who swallowed a goat.
42+
Just opened her throat and swallowed a goat!
43+
She swallowed the goat to catch the dog.
44+
She swallowed the dog to catch the cat.
45+
She swallowed the cat to catch the bird.
46+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
47+
She swallowed the spider to catch the fly.
48+
I don't know why she swallowed the fly. Perhaps she'll die.
49+
50+
I know an old lady who swallowed a cow.
51+
I don't know how she swallowed a cow!
52+
She swallowed the cow to catch the goat.
53+
She swallowed the goat to catch the dog.
54+
She swallowed the dog to catch the cat.
55+
She swallowed the cat to catch the bird.
56+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
57+
She swallowed the spider to catch the fly.
58+
I don't know why she swallowed the fly. Perhaps she'll die.
59+
60+
I know an old lady who swallowed a horse.
61+
She's dead, of course!
62+
```
63+
64+
[cumulative-song]: https://en.wikipedia.org/wiki/Cumulative_song
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+
"food-chain.sql"
8+
],
9+
"test": [
10+
"food-chain_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
14+
]
15+
},
16+
"blurb": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly"
19+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
DROP TABLE IF EXISTS chainmap;
2+
CREATE TEMPORARY TABLE chainmap (
3+
id INTEGER NOT NULL,
4+
animal TEXT NOT NULL,
5+
phrase TEXT NOT NULL
6+
);
7+
INSERT INTO chainmap (id, animal, phrase)
8+
VALUES
9+
(1, 'fly' , 'I don''t know why she swallowed the fly. Perhaps she''ll die.'),
10+
(2, 'spider', 'It wriggled and jiggled and tickled inside her.' ),
11+
(3, 'bird' , 'How absurd to swallow a bird!' ),
12+
(4, 'cat' , 'Imagine that, to swallow a cat!' ),
13+
(5, 'dog' , 'What a hog, to swallow a dog!' ),
14+
(6, 'goat' , 'Just opened her throat and swallowed a goat!' ),
15+
(7, 'cow' , 'I don''t know how she swallowed a cow!' ),
16+
(8, 'horse' , 'She''s dead, of course!' );
17+
18+
19+
DROP TABLE IF EXISTS lyrics;
20+
CREATE TEMPORARY TABLE lyrics (
21+
id INTEGER NOT NULL,
22+
verse TEXT NOT NULL
23+
);
24+
25+
WITH verses (id, verse) AS (
26+
WITH RECURSIVE reciter (idx, verses) AS (
27+
VALUES (1, '[]')
28+
UNION ALL
29+
SELECT
30+
idx + 1,
31+
JSON_INSERT(
32+
verses, '$[#]', (
33+
(WITH lines (line) AS (
34+
SELECT
35+
PRINTF(
36+
'I know an old lady who swallowed a %s.',
37+
(SELECT animal FROM chainmap WHERE id = idx)
38+
)
39+
UNION ALL
40+
SELECT phrase FROM chainmap WHERE id = idx
41+
UNION ALL
42+
SELECT
43+
IIF(
44+
idx = 1,
45+
NULL,
46+
PRINTF(
47+
'She swallowed the %s to catch the %s',
48+
(SELECT animal FROM chainmap WHERE id = idx),
49+
(SELECT animal FROM chainmap WHERE id = idx - 1)
50+
)
51+
|| IIF(idx = 3,
52+
REPLACE(
53+
RTRIM(JSON_EXTRACT(verses, '$[1][1]'), '.'),
54+
'It ', ' that '), '')
55+
|| '.'
56+
)
57+
UNION ALL
58+
SELECT IIF(idx >= 4 AND j.key = 1, NULL, j.value)
59+
FROM
60+
JSON_EACH(
61+
JSON_EXTRACT(
62+
verses,
63+
PRINTF('$[%d]', IIF(idx = 1, 0, idx - 2)))
64+
) j
65+
WHERE j.key != 0
66+
AND j.value NOT LIKE
67+
'%It wriggled and jiggled and tickled inside her.%'
68+
) SELECT JSON_GROUP_ARRAY(line) FROM lines WHERE line NOTNULL)
69+
)
70+
)
71+
FROM reciter
72+
WHERE idx <= (SELECT COUNT(*) FROM chainmap)
73+
)
74+
SELECT j.key + 1, j.value
75+
FROM JSON_EACH((
76+
SELECT
77+
JSON_SET(
78+
verses,'$[#-1]', JSON_EXTRACT(verses, '$[#-1][0]', '$[#-1][1]')
79+
)
80+
FROM reciter
81+
ORDER BY idx DESC
82+
LIMIT 1
83+
)) j
84+
)
85+
INSERT INTO lyrics (id, verse)
86+
SELECT id,
87+
(SELECT GROUP_CONCAT(j.value, CHAR(10))
88+
FROM JSON_EACH(verse) j) AS verse
89+
FROM verses
90+
;
91+
92+
UPDATE "food-chain"
93+
SET result = (
94+
SELECT GROUP_CONCAT( verse, CHAR(10) || CHAR(10) )
95+
FROM lyrics
96+
WHERE id BETWEEN start_verse AND end_verse
97+
)
98+
;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
[751dce68-9412-496e-b6e8-855998c56166]
13+
description = "fly"
14+
15+
[6c56f861-0c5e-4907-9a9d-b2efae389379]
16+
description = "spider"
17+
18+
[3edf5f33-bef1-4e39-ae67-ca5eb79203fa]
19+
description = "bird"
20+
21+
[e866a758-e1ff-400e-9f35-f27f28cc288f]
22+
description = "cat"
23+
24+
[3f02c30e-496b-4b2a-8491-bc7e2953cafb]
25+
description = "dog"
26+
27+
[4b3fd221-01ea-46e0-825b-5734634fbc59]
28+
description = "goat"
29+
30+
[1b707da9-7001-4fac-941f-22ad9c7a65d4]
31+
description = "cow"
32+
33+
[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc]
34+
description = "horse"
35+
36+
[22b863d5-17e4-4d1e-93e4-617329a5c050]
37+
description = "multiple verses"
38+
39+
[e626b32b-745c-4101-bcbd-3b13456893db]
40+
description = "full song"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DROP TABLE IF EXISTS "food-chain";
2+
CREATE TABLE "food-chain" (
3+
start_verse INTEGER NOT NULL,
4+
end_verse INTEGER NOT NULL,
5+
result TEXT
6+
);
7+
8+
.mode csv
9+
.import ./data.csv "food-chain"
10+
11+
UPDATE "food-chain" SET result = NULL;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
start_verse INTEGER NOT NULL,
14+
end_verse INTEGER NOT NULL,
15+
expected TEXT NOT NULL
16+
);
17+
18+
INSERT INTO tests (uuid, description, start_verse, end_verse, expected)
19+
VALUES
20+
('751dce68-9412-496e-b6e8-855998c56166', 'fly', 1, 1, 'I know an old lady who swallowed a fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.'),
21+
('6c56f861-0c5e-4907-9a9d-b2efae389379', 'spider', 2, 2, 'I know an old lady who swallowed a spider.\nIt wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.'),
22+
('3edf5f33-bef1-4e39-ae67-ca5eb79203fa', 'bird', 3, 3, 'I know an old lady who swallowed a bird.\nHow absurd to swallow a bird!\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.'),
23+
('e866a758-e1ff-400e-9f35-f27f28cc288f', 'cat', 4, 4, 'I know an old lady who swallowed a cat.\nImagine that, to swallow a cat!\nShe swallowed the cat to catch the bird.\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.'),
24+
('3f02c30e-496b-4b2a-8491-bc7e2953cafb', 'dog', 5, 5, 'I know an old lady who swallowed a dog.\nWhat a hog, to swallow a dog!\nShe swallowed the dog to catch the cat.\nShe swallowed the cat to catch the bird.\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.'),
25+
('4b3fd221-01ea-46e0-825b-5734634fbc59', 'goat', 6, 6, 'I know an old lady who swallowed a goat.\nJust opened her throat and swallowed a goat!\nShe swallowed the goat to catch the dog.\nShe swallowed the dog to catch the cat.\nShe swallowed the cat to catch the bird.\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.'),
26+
('1b707da9-7001-4fac-941f-22ad9c7a65d4', 'cow', 7, 7, 'I know an old lady who swallowed a cow.\nI don''t know how she swallowed a cow!\nShe swallowed the cow to catch the goat.\nShe swallowed the goat to catch the dog.\nShe swallowed the dog to catch the cat.\nShe swallowed the cat to catch the bird.\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.'),
27+
('3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc', 'horse', 8, 8, 'I know an old lady who swallowed a horse.\nShe''s dead, of course!'),
28+
('22b863d5-17e4-4d1e-93e4-617329a5c050', 'multiple verses', 1, 3, 'I know an old lady who swallowed a fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.\n\nI know an old lady who swallowed a spider.\nIt wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.\n\nI know an old lady who swallowed a bird.\nHow absurd to swallow a bird!\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.'),
29+
('e626b32b-745c-4101-bcbd-3b13456893db', 'full song', 1, 8, 'I know an old lady who swallowed a fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.\n\nI know an old lady who swallowed a spider.\nIt wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.\n\nI know an old lady who swallowed a bird.\nHow absurd to swallow a bird!\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.\n\nI know an old lady who swallowed a cat.\nImagine that, to swallow a cat!\nShe swallowed the cat to catch the bird.\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.\n\nI know an old lady who swallowed a dog.\nWhat a hog, to swallow a dog!\nShe swallowed the dog to catch the cat.\nShe swallowed the cat to catch the bird.\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.\n\nI know an old lady who swallowed a goat.\nJust opened her throat and swallowed a goat!\nShe swallowed the goat to catch the dog.\nShe swallowed the dog to catch the cat.\nShe swallowed the cat to catch the bird.\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.\n\nI know an old lady who swallowed a cow.\nI don''t know how she swallowed a cow!\nShe swallowed the cow to catch the goat.\nShe swallowed the goat to catch the dog.\nShe swallowed the dog to catch the cat.\nShe swallowed the cat to catch the bird.\nShe swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.\n\nI know an old lady who swallowed a horse.\nShe''s dead, of course!');
30+
31+
UPDATE tests SET expected = REPLACE(expected, '\n', CHAR(10));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1,1,
2+
2,2,
3+
3,3,
4+
4,4,
5+
5,5,
6+
6,6,
7+
7,7,
8+
8,8,
9+
1,3,
10+
1,8,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Schema:
2+
-- CREATE TABLE "food-chain" (
3+
-- start_verse INTEGER NOT NULL,
4+
-- end_verse INTEGER NOT NULL,
5+
-- result TEXT
6+
-- );
7+
--
8+
-- Task: update the food-chain table and set the result column based on the start_verse and end_verse.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- Create database:
2+
.read ./create_fixture.sql
3+
4+
-- Read user student solution and save any output as markdown in user_output.md:
5+
.mode markdown
6+
.output user_output.md
7+
.read ./food-chain.sql
8+
.output
9+
10+
-- Create a clean testing environment:
11+
.read ./create_test_table.sql
12+
13+
-- Comparison of user input and the tests updates the status for each test:
14+
UPDATE tests
15+
SET status = 'pass'
16+
FROM (SELECT start_verse, end_verse, result FROM "food-chain") AS actual
17+
WHERE (actual.start_verse, actual.end_verse, actual.result ) =
18+
( tests.start_verse, tests.end_verse, tests.expected)
19+
;
20+
21+
-- Update message for failed tests to give helpful information:
22+
UPDATE tests
23+
SET message = (
24+
'Result for "'
25+
|| PRINTF('start_verse=%d, end_verse=%d',
26+
tests.start_verse, tests.end_verse)
27+
|| '"' || ' is <' || COALESCE(actual.result, 'NULL')
28+
|| '> but should be <' || tests.expected || '>'
29+
)
30+
FROM (SELECT start_verse, end_verse, result FROM "food-chain") AS actual
31+
WHERE (actual.start_verse, actual.end_verse) =
32+
(tests.start_verse, tests.end_verse)
33+
AND tests.status = 'fail'
34+
;
35+
36+
-- Save results to ./output.json (needed by the online test-runner)
37+
.mode json
38+
.once './output.json'
39+
SELECT description, status, message, output, test_code, task_id FROM tests;
40+
41+
-- Display test results in readable form for the student:
42+
.mode table
43+
SELECT description, status, message FROM tests;

0 commit comments

Comments
 (0)