diff --git a/config.json b/config.json index 93d0e8a..1b01ca9 100644 --- a/config.json +++ b/config.json @@ -370,6 +370,14 @@ "prerequisites": [], "difficulty": 8 }, + { + "slug": "food-chain", + "name": "Food Chain", + "uuid": "128ab281-7720-4383-be90-a2b311bd8c38", + "practices": [], + "prerequisites": [], + "difficulty": 8 + }, { "slug": "hamming", "name": "Hamming", diff --git a/exercises/practice/food-chain/.docs/instructions.md b/exercises/practice/food-chain/.docs/instructions.md new file mode 100644 index 0000000..125820e --- /dev/null +++ b/exercises/practice/food-chain/.docs/instructions.md @@ -0,0 +1,64 @@ +# Instructions + +Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'. + +While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically. + +This is a [cumulative song][cumulative-song] of unknown origin. + +This is one of many common variants. + +```text +I know an old lady who swallowed a fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a spider. +It wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a bird. +How absurd to swallow a bird! +She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a cat. +Imagine that, to swallow a cat! +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a dog. +What a hog, to swallow a dog! +She swallowed the dog to catch the cat. +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a goat. +Just opened her throat and swallowed a goat! +She swallowed the goat to catch the dog. +She swallowed the dog to catch the cat. +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a cow. +I don't know how she swallowed a cow! +She swallowed the cow to catch the goat. +She swallowed the goat to catch the dog. +She swallowed the dog to catch the cat. +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a horse. +She's dead, of course! +``` + +[cumulative-song]: https://en.wikipedia.org/wiki/Cumulative_song diff --git a/exercises/practice/food-chain/.meta/config.json b/exercises/practice/food-chain/.meta/config.json new file mode 100644 index 0000000..c7ac46e --- /dev/null +++ b/exercises/practice/food-chain/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "jimmytty" + ], + "files": { + "solution": [ + "food-chain.sql" + ], + "test": [ + "food-chain_test.sql" + ], + "example": [ + ".meta/example.sql" + ] + }, + "blurb": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly" +} diff --git a/exercises/practice/food-chain/.meta/example.sql b/exercises/practice/food-chain/.meta/example.sql new file mode 100644 index 0000000..8980e42 --- /dev/null +++ b/exercises/practice/food-chain/.meta/example.sql @@ -0,0 +1,98 @@ +DROP TABLE IF EXISTS chainmap; +CREATE TEMPORARY TABLE chainmap ( + id INTEGER NOT NULL, + animal TEXT NOT NULL, + phrase TEXT NOT NULL +); +INSERT INTO chainmap (id, animal, phrase) +VALUES +(1, 'fly' , 'I don''t know why she swallowed the fly. Perhaps she''ll die.'), +(2, 'spider', 'It wriggled and jiggled and tickled inside her.' ), +(3, 'bird' , 'How absurd to swallow a bird!' ), +(4, 'cat' , 'Imagine that, to swallow a cat!' ), +(5, 'dog' , 'What a hog, to swallow a dog!' ), +(6, 'goat' , 'Just opened her throat and swallowed a goat!' ), +(7, 'cow' , 'I don''t know how she swallowed a cow!' ), +(8, 'horse' , 'She''s dead, of course!' ); + + +DROP TABLE IF EXISTS lyrics; +CREATE TEMPORARY TABLE lyrics ( + id INTEGER NOT NULL, + verse TEXT NOT NULL +); + +WITH verses (id, verse) AS ( + WITH RECURSIVE reciter (idx, verses) AS ( + VALUES (1, '[]') + UNION ALL + SELECT + idx + 1, + JSON_INSERT( + verses, '$[#]', ( + (WITH lines (line) AS ( + SELECT + PRINTF( + 'I know an old lady who swallowed a %s.', + (SELECT animal FROM chainmap WHERE id = idx) + ) + UNION ALL + SELECT phrase FROM chainmap WHERE id = idx + UNION ALL + SELECT + IIF( + idx = 1, + NULL, + PRINTF( + 'She swallowed the %s to catch the %s', + (SELECT animal FROM chainmap WHERE id = idx), + (SELECT animal FROM chainmap WHERE id = idx - 1) + ) + || IIF(idx = 3, + REPLACE( + RTRIM(JSON_EXTRACT(verses, '$[1][1]'), '.'), + 'It ', ' that '), '') + || '.' + ) + UNION ALL + SELECT IIF(idx >= 4 AND j.key = 1, NULL, j.value) + FROM + JSON_EACH( + JSON_EXTRACT( + verses, + PRINTF('$[%d]', IIF(idx = 1, 0, idx - 2))) + ) j + WHERE j.key != 0 + AND j.value NOT LIKE + '%It wriggled and jiggled and tickled inside her.%' + ) SELECT JSON_GROUP_ARRAY(line) FROM lines WHERE line NOTNULL) + ) + ) + FROM reciter + WHERE idx <= (SELECT COUNT(*) FROM chainmap) + ) + SELECT j.key + 1, j.value + FROM JSON_EACH(( + SELECT + JSON_SET( + verses,'$[#-1]', JSON_EXTRACT(verses, '$[#-1][0]', '$[#-1][1]') + ) + FROM reciter + ORDER BY idx DESC + LIMIT 1 + )) j +) +INSERT INTO lyrics (id, verse) +SELECT id, + (SELECT GROUP_CONCAT(j.value, CHAR(10)) + FROM JSON_EACH(verse) j) AS verse + FROM verses +; + +UPDATE "food-chain" + SET result = ( + SELECT GROUP_CONCAT( verse, CHAR(10) || CHAR(10) ) + FROM lyrics + WHERE id BETWEEN start_verse AND end_verse + ) +; diff --git a/exercises/practice/food-chain/.meta/tests.toml b/exercises/practice/food-chain/.meta/tests.toml new file mode 100644 index 0000000..30c5b98 --- /dev/null +++ b/exercises/practice/food-chain/.meta/tests.toml @@ -0,0 +1,40 @@ +# 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. + +[751dce68-9412-496e-b6e8-855998c56166] +description = "fly" + +[6c56f861-0c5e-4907-9a9d-b2efae389379] +description = "spider" + +[3edf5f33-bef1-4e39-ae67-ca5eb79203fa] +description = "bird" + +[e866a758-e1ff-400e-9f35-f27f28cc288f] +description = "cat" + +[3f02c30e-496b-4b2a-8491-bc7e2953cafb] +description = "dog" + +[4b3fd221-01ea-46e0-825b-5734634fbc59] +description = "goat" + +[1b707da9-7001-4fac-941f-22ad9c7a65d4] +description = "cow" + +[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc] +description = "horse" + +[22b863d5-17e4-4d1e-93e4-617329a5c050] +description = "multiple verses" + +[e626b32b-745c-4101-bcbd-3b13456893db] +description = "full song" diff --git a/exercises/practice/food-chain/create_fixture.sql b/exercises/practice/food-chain/create_fixture.sql new file mode 100644 index 0000000..83b2871 --- /dev/null +++ b/exercises/practice/food-chain/create_fixture.sql @@ -0,0 +1,11 @@ +DROP TABLE IF EXISTS "food-chain"; +CREATE TABLE "food-chain" ( + start_verse INTEGER NOT NULL, + end_verse INTEGER NOT NULL, + result TEXT +); + +.mode csv +.import ./data.csv "food-chain" + +UPDATE "food-chain" SET result = NULL; diff --git a/exercises/practice/food-chain/create_test_table.sql b/exercises/practice/food-chain/create_test_table.sql new file mode 100644 index 0000000..5dc2a88 --- /dev/null +++ b/exercises/practice/food-chain/create_test_table.sql @@ -0,0 +1,31 @@ +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 + start_verse INTEGER NOT NULL, + end_verse INTEGER NOT NULL, + expected TEXT NOT NULL +); + +INSERT INTO tests (uuid, description, start_verse, end_verse, expected) +VALUES +('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.'), +('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.'), +('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.'), +('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.'), +('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.'), +('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.'), +('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.'), +('3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc', 'horse', 8, 8, 'I know an old lady who swallowed a horse.\nShe''s dead, of course!'), +('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.'), +('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!'); + +UPDATE tests SET expected = REPLACE(expected, '\n', CHAR(10)); diff --git a/exercises/practice/food-chain/data.csv b/exercises/practice/food-chain/data.csv new file mode 100644 index 0000000..4989ffb --- /dev/null +++ b/exercises/practice/food-chain/data.csv @@ -0,0 +1,10 @@ +1,1, +2,2, +3,3, +4,4, +5,5, +6,6, +7,7, +8,8, +1,3, +1,8, diff --git a/exercises/practice/food-chain/food-chain.sql b/exercises/practice/food-chain/food-chain.sql new file mode 100644 index 0000000..57ed8ab --- /dev/null +++ b/exercises/practice/food-chain/food-chain.sql @@ -0,0 +1,8 @@ +-- Schema: +-- CREATE TABLE "food-chain" ( +-- start_verse INTEGER NOT NULL, +-- end_verse INTEGER NOT NULL, +-- result TEXT +-- ); +-- +-- Task: update the food-chain table and set the result column based on the start_verse and end_verse. diff --git a/exercises/practice/food-chain/food-chain_test.sql b/exercises/practice/food-chain/food-chain_test.sql new file mode 100644 index 0000000..e6c0327 --- /dev/null +++ b/exercises/practice/food-chain/food-chain_test.sql @@ -0,0 +1,43 @@ +-- Create database: +.read ./create_fixture.sql + +-- Read user student solution and save any output as markdown in user_output.md: +.mode markdown +.output user_output.md +.read ./food-chain.sql +.output + +-- Create a clean testing environment: +.read ./create_test_table.sql + +-- Comparison of user input and the tests updates the status for each test: +UPDATE tests + SET status = 'pass' + FROM (SELECT start_verse, end_verse, result FROM "food-chain") AS actual + WHERE (actual.start_verse, actual.end_verse, actual.result ) = + ( tests.start_verse, tests.end_verse, tests.expected) +; + +-- Update message for failed tests to give helpful information: +UPDATE tests + SET message = ( + 'Result for "' + || PRINTF('start_verse=%d, end_verse=%d', + tests.start_verse, tests.end_verse) + || '"' || ' is <' || COALESCE(actual.result, 'NULL') + || '> but should be <' || tests.expected || '>' + ) + FROM (SELECT start_verse, end_verse, result FROM "food-chain") AS actual + WHERE (actual.start_verse, actual.end_verse) = + (tests.start_verse, tests.end_verse) + AND tests.status = 'fail' +; + +-- Save results to ./output.json (needed by the online test-runner) +.mode json +.once './output.json' +SELECT description, status, message, output, test_code, task_id FROM tests; + +-- Display test results in readable form for the student: +.mode table +SELECT description, status, message FROM tests;