-
-
Notifications
You must be signed in to change notification settings - Fork 13
Add new practice exercise: bottle-song #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9dced7f
* add new practice exercise: bottle-song
jimmytty 263f64b
Merge branch 'main' into bottle-song
jimmytty 0c372ff
* trying PRINTF() instead FORMAT()
jimmytty 3c6412e
* adjusting code style
jimmytty be8bff8
Update exercises/practice/bottle-song/bottle-song.sql
jimmytty 535750c
* indentation
jimmytty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Instructions | ||
|
||
Recite the lyrics to that popular children's repetitive song: Ten Green Bottles. | ||
|
||
Note that not all verses are identical. | ||
|
||
```text | ||
Ten green bottles hanging on the wall, | ||
Ten green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There'll be nine green bottles hanging on the wall. | ||
|
||
Nine green bottles hanging on the wall, | ||
Nine green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There'll be eight green bottles hanging on the wall. | ||
|
||
Eight green bottles hanging on the wall, | ||
Eight green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There'll be seven green bottles hanging on the wall. | ||
|
||
Seven green bottles hanging on the wall, | ||
Seven green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There'll be six green bottles hanging on the wall. | ||
|
||
Six green bottles hanging on the wall, | ||
Six green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There'll be five green bottles hanging on the wall. | ||
|
||
Five green bottles hanging on the wall, | ||
Five green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There'll be four green bottles hanging on the wall. | ||
|
||
Four green bottles hanging on the wall, | ||
Four green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There'll be three green bottles hanging on the wall. | ||
|
||
Three green bottles hanging on the wall, | ||
Three green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There'll be two green bottles hanging on the wall. | ||
|
||
Two green bottles hanging on the wall, | ||
Two green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There'll be one green bottle hanging on the wall. | ||
|
||
One green bottle hanging on the wall, | ||
One green bottle hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There'll be no green bottles hanging on the wall. | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"jimmytty" | ||
], | ||
"files": { | ||
"solution": [ | ||
"bottle-song.sql" | ||
], | ||
"test": [ | ||
"bottle-song_test.sql" | ||
], | ||
"example": [ | ||
".meta/example.sql" | ||
] | ||
}, | ||
"blurb": "Produce the lyrics to the popular children's repetitive song: Ten Green Bottles.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/Ten_Green_Bottles" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
DROP TABLE IF EXISTS pairs; | ||
CREATE TEMPORARY TABLE pairs ( | ||
n INTEGER NOT NULL, | ||
a TEXT NOT NULL, | ||
b TEXT NOT NULL | ||
); | ||
INSERT INTO pairs (n, a, b) | ||
VALUES | ||
(10, 'Ten' , 'Nine' ), | ||
jimmytty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
( 9, 'Nine' , 'Eight'), | ||
( 8, 'Eight', 'Seven'), | ||
( 7, 'Seven', 'Six' ), | ||
( 6, 'Six' , 'Five' ), | ||
( 5, 'Five' , 'Four' ), | ||
( 4, 'Four' , 'Three'), | ||
( 3, 'Three', 'Two' ), | ||
( 2, 'Two' , 'One' ), | ||
( 1, 'One' , 'no' ); | ||
|
||
DROP TABLE IF EXISTS verses; | ||
CREATE TABLE verses AS | ||
SELECT n, | ||
PRINTF('%s green bottles hanging on the wall,' || CHAR(10), a) || | ||
PRINTF('%s green bottles hanging on the wall,' || CHAR(10), a) || | ||
'And if one green bottle should accidentally fall,' || CHAR(10) || | ||
PRINTF( | ||
'There''ll be %s green bottles hanging on the wall.', | ||
LOWER(b) | ||
) AS verse | ||
FROM pairs | ||
; | ||
|
||
UPDATE verses | ||
SET verse = REPLACE(verse, 'be one green bottles', 'be one green bottle') | ||
WHERE n = 2; | ||
UPDATE verses | ||
SET verse = REPLACE(verse, 'One green bottles', 'One green bottle') | ||
WHERE n = 1; | ||
|
||
|
||
UPDATE "bottle-song" | ||
SET result = ( | ||
SELECT GROUP_CONCAT(verse, CHAR(10)||CHAR(10)) | ||
FROM ( | ||
SELECT verse | ||
FROM verses | ||
WHERE n <= start_bottles | ||
AND n > start_bottles - take_down | ||
ORDER BY n DESC | ||
) | ||
) | ||
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 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. | ||
|
||
[d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03] | ||
description = "verse -> single verse -> first generic verse" | ||
|
||
[0f0aded3-472a-4c64-b842-18d4f1f5f030] | ||
description = "verse -> single verse -> last generic verse" | ||
|
||
[f61f3c97-131f-459e-b40a-7428f3ed99d9] | ||
description = "verse -> single verse -> verse with 2 bottles" | ||
|
||
[05eadba9-5dbd-401e-a7e8-d17cc9baa8e0] | ||
description = "verse -> single verse -> verse with 1 bottle" | ||
|
||
[a4a28170-83d6-4dc1-bd8b-319b6abb6a80] | ||
description = "lyrics -> multiple verses -> first two verses" | ||
|
||
[3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db] | ||
description = "lyrics -> multiple verses -> last three verses" | ||
|
||
[28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28] | ||
description = "lyrics -> multiple verses -> all verses" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- Schema: CREATE TABLE "bottle-song" ( | ||
-- start_bottles INTEGER NOT NULL, | ||
-- take_down INTEGER NOT NULL, | ||
-- result TEXT | ||
-- ); | ||
-- Task: update bottle-song table and set the result based on the | ||
-- start_bottles and take_down. | ||
jimmytty marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
-- 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 ./bottle-song.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_bottles, take_down, result FROM "bottle-song") AS actual | ||
WHERE (actual.start_bottles, actual.take_down, actual.result) = (tests.start_bottles, tests.take_down, tests.expected); | ||
|
||
-- Update message for failed tests to give helpful information: | ||
UPDATE tests | ||
SET message = ( | ||
'Result for "' | ||
|| PRINTF( | ||
'start_bottles=%d, take_down=%d', | ||
jimmytty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tests.start_bottles, | ||
tests.take_down | ||
) | ||
|| '"' | ||
|| ' is <' || COALESCE(actual.result, 'NULL') | ||
|| '> but should be <' || tests.expected || '>' | ||
) | ||
FROM (SELECT start_bottles, take_down, result FROM "bottle-song") AS actual | ||
WHERE (actual.start_bottles, actual.take_down) = (tests.start_bottles, tests.take_down) 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
DROP TABLE IF EXISTS "bottle-song"; | ||
CREATE TABLE "bottle-song" ( | ||
start_bottles INTEGER NOT NULL, | ||
take_down INTEGER NOT NULL, | ||
result TEXT | ||
); | ||
|
||
.mode csv | ||
.import ./data.csv "bottle-song" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
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_bottles INTEGER NOT NULL, | ||
take_down INTEGER NOT NULL, | ||
expected TEXT NOT NULL | ||
); | ||
|
||
INSERT INTO tests (uuid, description, start_bottles, take_down, expected) | ||
VALUES | ||
('d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03','first generic verse',10,1,'Ten green bottles hanging on the wall, | ||
Ten green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be nine green bottles hanging on the wall.'), | ||
('0f0aded3-472a-4c64-b842-18d4f1f5f030','last generic verse',3,1,'Three green bottles hanging on the wall, | ||
Three green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be two green bottles hanging on the wall.'), | ||
('f61f3c97-131f-459e-b40a-7428f3ed99d9','verse with 2 bottles',2,1,'Two green bottles hanging on the wall, | ||
Two green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be one green bottle hanging on the wall.'), | ||
('05eadba9-5dbd-401e-a7e8-d17cc9baa8e0','verse with 1 bottle',1,1,'One green bottle hanging on the wall, | ||
One green bottle hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be no green bottles hanging on the wall.'), | ||
('a4a28170-83d6-4dc1-bd8b-319b6abb6a80','first two verses',10,2,'Ten green bottles hanging on the wall, | ||
Ten green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be nine green bottles hanging on the wall. | ||
|
||
Nine green bottles hanging on the wall, | ||
Nine green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be eight green bottles hanging on the wall.'), | ||
('3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db','last three verses',3,3,'Three green bottles hanging on the wall, | ||
Three green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be two green bottles hanging on the wall. | ||
|
||
Two green bottles hanging on the wall, | ||
Two green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be one green bottle hanging on the wall. | ||
|
||
One green bottle hanging on the wall, | ||
One green bottle hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be no green bottles hanging on the wall.'), | ||
('28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28','all verses',10,10,'Ten green bottles hanging on the wall, | ||
Ten green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be nine green bottles hanging on the wall. | ||
|
||
Nine green bottles hanging on the wall, | ||
Nine green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be eight green bottles hanging on the wall. | ||
|
||
Eight green bottles hanging on the wall, | ||
Eight green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be seven green bottles hanging on the wall. | ||
|
||
Seven green bottles hanging on the wall, | ||
Seven green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be six green bottles hanging on the wall. | ||
|
||
Six green bottles hanging on the wall, | ||
Six green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be five green bottles hanging on the wall. | ||
|
||
Five green bottles hanging on the wall, | ||
Five green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be four green bottles hanging on the wall. | ||
|
||
Four green bottles hanging on the wall, | ||
Four green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be three green bottles hanging on the wall. | ||
|
||
Three green bottles hanging on the wall, | ||
Three green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be two green bottles hanging on the wall. | ||
|
||
Two green bottles hanging on the wall, | ||
Two green bottles hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be one green bottle hanging on the wall. | ||
|
||
One green bottle hanging on the wall, | ||
One green bottle hanging on the wall, | ||
And if one green bottle should accidentally fall, | ||
There''ll be no green bottles hanging on the wall.'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
10,1,"" | ||
3,1,"" | ||
2,1,"" | ||
1,1,"" | ||
10,2,"" | ||
3,3,"" | ||
10,10,"" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.