From 137d80f6552f6f526406fb4d91674bfe97cc3d82 Mon Sep 17 00:00:00 2001 From: Ronaldo Ferreira de Lima Date: Wed, 20 Aug 2025 22:26:44 -0300 Subject: [PATCH] * add new practice exercise: house --- config.json | 8 ++ .../practice/house/.docs/instructions.md | 105 ++++++++++++++++++ exercises/practice/house/.meta/config.json | 19 ++++ exercises/practice/house/.meta/example.sql | 38 +++++++ exercises/practice/house/.meta/tests.toml | 52 +++++++++ exercises/practice/house/create_fixture.sql | 11 ++ .../practice/house/create_test_table.sql | 35 ++++++ exercises/practice/house/data.csv | 14 +++ exercises/practice/house/house.sql | 8 ++ exercises/practice/house/house_test.sql | 40 +++++++ 10 files changed, 330 insertions(+) create mode 100644 exercises/practice/house/.docs/instructions.md create mode 100644 exercises/practice/house/.meta/config.json create mode 100644 exercises/practice/house/.meta/example.sql create mode 100644 exercises/practice/house/.meta/tests.toml create mode 100644 exercises/practice/house/create_fixture.sql create mode 100644 exercises/practice/house/create_test_table.sql create mode 100644 exercises/practice/house/data.csv create mode 100644 exercises/practice/house/house.sql create mode 100644 exercises/practice/house/house_test.sql diff --git a/config.json b/config.json index ea86b75..7a28295 100644 --- a/config.json +++ b/config.json @@ -194,6 +194,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "house", + "name": "House", + "uuid": "e684299c-9db3-4dce-b3c7-53c9aa38ceaa", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "kindergarten-garden", "name": "Kindergarten Garden", diff --git a/exercises/practice/house/.docs/instructions.md b/exercises/practice/house/.docs/instructions.md new file mode 100644 index 0000000..88928c5 --- /dev/null +++ b/exercises/practice/house/.docs/instructions.md @@ -0,0 +1,105 @@ +# Instructions + +Recite the nursery rhyme 'This is the House that Jack Built'. + +> [The] process of placing a phrase of clause within another phrase of clause is called embedding. +> It is through the processes of recursion and embedding that we are able to take a finite number of forms (words and phrases) and construct an infinite number of expressions. +> Furthermore, embedding also allows us to construct an infinitely long structure, in theory anyway. + +- [papyr.com][papyr] + +The nursery rhyme reads as follows: + +```text +This is the house that Jack built. + +This is the malt +that lay in the house that Jack built. + +This is the rat +that ate the malt +that lay in the house that Jack built. + +This is the cat +that killed the rat +that ate the malt +that lay in the house that Jack built. + +This is the dog +that worried the cat +that killed the rat +that ate the malt +that lay in the house that Jack built. + +This is the cow with the crumpled horn +that tossed the dog +that worried the cat +that killed the rat +that ate the malt +that lay in the house that Jack built. + +This is the maiden all forlorn +that milked the cow with the crumpled horn +that tossed the dog +that worried the cat +that killed the rat +that ate the malt +that lay in the house that Jack built. + +This is the man all tattered and torn +that kissed the maiden all forlorn +that milked the cow with the crumpled horn +that tossed the dog +that worried the cat +that killed the rat +that ate the malt +that lay in the house that Jack built. + +This is the priest all shaven and shorn +that married the man all tattered and torn +that kissed the maiden all forlorn +that milked the cow with the crumpled horn +that tossed the dog +that worried the cat +that killed the rat +that ate the malt +that lay in the house that Jack built. + +This is the rooster that crowed in the morn +that woke the priest all shaven and shorn +that married the man all tattered and torn +that kissed the maiden all forlorn +that milked the cow with the crumpled horn +that tossed the dog +that worried the cat +that killed the rat +that ate the malt +that lay in the house that Jack built. + +This is the farmer sowing his corn +that kept the rooster that crowed in the morn +that woke the priest all shaven and shorn +that married the man all tattered and torn +that kissed the maiden all forlorn +that milked the cow with the crumpled horn +that tossed the dog +that worried the cat +that killed the rat +that ate the malt +that lay in the house that Jack built. + +This is the horse and the hound and the horn +that belonged to the farmer sowing his corn +that kept the rooster that crowed in the morn +that woke the priest all shaven and shorn +that married the man all tattered and torn +that kissed the maiden all forlorn +that milked the cow with the crumpled horn +that tossed the dog +that worried the cat +that killed the rat +that ate the malt +that lay in the house that Jack built. +``` + +[papyr]: https://papyr.com/hypertextbooks/grammar/ph_noun.htm diff --git a/exercises/practice/house/.meta/config.json b/exercises/practice/house/.meta/config.json new file mode 100644 index 0000000..d98705c --- /dev/null +++ b/exercises/practice/house/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "jimmytty" + ], + "files": { + "solution": [ + "house.sql" + ], + "test": [ + "house_test.sql" + ], + "example": [ + ".meta/example.sql" + ] + }, + "blurb": "Output the nursery rhyme 'This is the House that Jack Built'.", + "source": "British nursery rhyme", + "source_url": "https://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built" +} diff --git a/exercises/practice/house/.meta/example.sql b/exercises/practice/house/.meta/example.sql new file mode 100644 index 0000000..93230b7 --- /dev/null +++ b/exercises/practice/house/.meta/example.sql @@ -0,0 +1,38 @@ +DROP TABLE IF EXISTS verses; +CREATE TEMPORARY TABLE verses AS +WITH cte (idx, sentence) AS ( + VALUES + ( 1, 'in the house that Jack built' ), + ( 2, 'malt that lay' ), + ( 3, 'rat that ate the' ), + ( 4, 'cat that killed the' ), + ( 5, 'dog that worried the' ), + ( 6, 'cow with the crumpled horn that tossed the' ), + ( 7, 'maiden all forlorn that milked the' ), + ( 8, 'man all tattered and torn that kissed the' ), + ( 9, 'priest all shaven and shorn that married the' ), + (10, 'rooster that crowed in the morn that woke the' ), + (11, 'farmer sowing his corn that kept the' ), + (12, 'horse and the hound and the horn that belonged to the' ) +) +SELECT idx, + IIF(idx = 1, REPLACE(verse, ' the in the ', ' the '), verse) AS verse + FROM ( + SELECT idx, + PRINTF( + 'This is the %s.', + GROUP_CONCAT(sentence, ' ') + OVER (ORDER BY idx DESC ROWS BETWEEN 0 PRECEDING AND 12 FOLLOWING) + ) AS verse + FROM cte + ORDER BY idx ASC + ) +; + +UPDATE house + SET result = ( + SELECT GROUP_CONCAT(verse, CHAR(10)) + FROM verses + WHERE idx BETWEEN start_verse AND end_verse + ) +; diff --git a/exercises/practice/house/.meta/tests.toml b/exercises/practice/house/.meta/tests.toml new file mode 100644 index 0000000..da24dc3 --- /dev/null +++ b/exercises/practice/house/.meta/tests.toml @@ -0,0 +1,52 @@ +# 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. + +[28a540ff-f765-4348-9d57-ae33f25f41f2] +description = "verse one - the house that jack built" + +[ebc825ac-6e2b-4a5e-9afd-95732191c8da] +description = "verse two - the malt that lay" + +[1ed8bb0f-edb8-4bd1-b6d4-b64754fe4a60] +description = "verse three - the rat that ate" + +[64b0954e-8b7d-4d14-aad0-d3f6ce297a30] +description = "verse four - the cat that killed" + +[1e8d56bc-fe31-424d-9084-61e6111d2c82] +description = "verse five - the dog that worried" + +[6312dc6f-ab0a-40c9-8a55-8d4e582beac4] +description = "verse six - the cow with the crumpled horn" + +[68f76d18-6e19-4692-819c-5ff6a7f92feb] +description = "verse seven - the maiden all forlorn" + +[73872564-2004-4071-b51d-2e4326096747] +description = "verse eight - the man all tattered and torn" + +[0d53d743-66cb-4351-a173-82702f3338c9] +description = "verse nine - the priest all shaven and shorn" + +[452f24dc-8fd7-4a82-be1a-3b4839cfeb41] +description = "verse 10 - the rooster that crowed in the morn" + +[97176f20-2dd3-4646-ac72-cffced91ea26] +description = "verse 11 - the farmer sowing his corn" + +[09824c29-6aad-4dcd-ac98-f61374a6a8b7] +description = "verse 12 - the horse and the hound and the horn" + +[d2b980d3-7851-49e1-97ab-1524515ec200] +description = "multiple verses" + +[0311d1d0-e085-4f23-8ae7-92406fb3e803] +description = "full rhyme" diff --git a/exercises/practice/house/create_fixture.sql b/exercises/practice/house/create_fixture.sql new file mode 100644 index 0000000..a3a2578 --- /dev/null +++ b/exercises/practice/house/create_fixture.sql @@ -0,0 +1,11 @@ +DROP TABLE IF EXISTS house; +CREATE TABLE house ( + start_verse INTEGER NOT NULL, + end_verse INTEGER NOT NULL, + result TEXT +); + +.mode csv +.import ./data.csv house + +UPDATE house SET result = NULL; diff --git a/exercises/practice/house/create_test_table.sql b/exercises/practice/house/create_test_table.sql new file mode 100644 index 0000000..8320096 --- /dev/null +++ b/exercises/practice/house/create_test_table.sql @@ -0,0 +1,35 @@ +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 + ('28a540ff-f765-4348-9d57-ae33f25f41f2', 'verse one - the house that jack built', 1, 1, 'This is the house that Jack built.'), + ('ebc825ac-6e2b-4a5e-9afd-95732191c8da', 'verse two - the malt that lay', 2, 2, 'This is the malt that lay in the house that Jack built.'), + ('1ed8bb0f-edb8-4bd1-b6d4-b64754fe4a60', 'verse three - the rat that ate', 3, 3, 'This is the rat that ate the malt that lay in the house that Jack built.'), + ('64b0954e-8b7d-4d14-aad0-d3f6ce297a30', 'verse four - the cat that killed', 4, 4, 'This is the cat that killed the rat that ate the malt that lay in the house that Jack built.'), + ('1e8d56bc-fe31-424d-9084-61e6111d2c82', 'verse five - the dog that worried', 5, 5, 'This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'), + ('6312dc6f-ab0a-40c9-8a55-8d4e582beac4', 'verse six - the cow with the crumpled horn', 6, 6, 'This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'), + ('68f76d18-6e19-4692-819c-5ff6a7f92feb', 'verse seven - the maiden all forlorn', 7, 7, 'This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'), + ('73872564-2004-4071-b51d-2e4326096747', 'verse eight - the man all tattered and torn', 8, 8, 'This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'), + ('0d53d743-66cb-4351-a173-82702f3338c9', 'verse nine - the priest all shaven and shorn', 9, 9, 'This is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'), + ('452f24dc-8fd7-4a82-be1a-3b4839cfeb41', 'verse 10 - the rooster that crowed in the morn', 10, 10, 'This is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'), + ('97176f20-2dd3-4646-ac72-cffced91ea26', 'verse 11 - the farmer sowing his corn', 11, 11, 'This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'), + ('09824c29-6aad-4dcd-ac98-f61374a6a8b7', 'verse 12 - the horse and the hound and the horn', 12, 12, 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'), + ('d2b980d3-7851-49e1-97ab-1524515ec200', 'multiple verses', 4, 8, 'This is the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'), + ('0311d1d0-e085-4f23-8ae7-92406fb3e803', 'full rhyme', 1, 12, 'This is the house that Jack built.\nThis is the malt that lay in the house that Jack built.\nThis is the rat that ate the malt that lay in the house that Jack built.\nThis is the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\nThis is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'); + +UPDATE tests SET expected = REPLACE(expected, '\n', CHAR(10)); diff --git a/exercises/practice/house/data.csv b/exercises/practice/house/data.csv new file mode 100644 index 0000000..7b1327a --- /dev/null +++ b/exercises/practice/house/data.csv @@ -0,0 +1,14 @@ +1,1, +2,2, +3,3, +4,4, +5,5, +6,6, +7,7, +8,8, +9,9, +10,10, +11,11, +12,12, +4,8, +1,12, diff --git a/exercises/practice/house/house.sql b/exercises/practice/house/house.sql new file mode 100644 index 0000000..79a3490 --- /dev/null +++ b/exercises/practice/house/house.sql @@ -0,0 +1,8 @@ +-- Schema: +-- CREATE TABLE house ( +-- start_verse INTEGER NOT NULL, +-- end_verse INTEGER NOT NULL, +-- result TEXT +-- ); +-- +-- Task: update house table and set the result based on the start_verse and end_verse. diff --git a/exercises/practice/house/house_test.sql b/exercises/practice/house/house_test.sql new file mode 100644 index 0000000..19c410d --- /dev/null +++ b/exercises/practice/house/house_test.sql @@ -0,0 +1,40 @@ +-- 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 ./house.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 house) 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 house) 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;