Skip to content

Commit 5ccf5eb

Browse files
authored
* add new practice exercise: bottle-song (#112)
1 parent a61bdbb commit 5ccf5eb

File tree

10 files changed

+334
-0
lines changed

10 files changed

+334
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
"prerequisites": [],
5151
"difficulty": 1
5252
},
53+
{
54+
"slug": "bottle-song",
55+
"name": "Bottle Song",
56+
"uuid": "a3961350-2053-4e08-bf5d-fe9b43cb753c",
57+
"practices": [],
58+
"prerequisites": [],
59+
"difficulty": 2
60+
},
5361
{
5462
"slug": "darts",
5563
"name": "Darts",
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Instructions
2+
3+
Recite the lyrics to that popular children's repetitive song: Ten Green Bottles.
4+
5+
Note that not all verses are identical.
6+
7+
```text
8+
Ten green bottles hanging on the wall,
9+
Ten green bottles hanging on the wall,
10+
And if one green bottle should accidentally fall,
11+
There'll be nine green bottles hanging on the wall.
12+
13+
Nine green bottles hanging on the wall,
14+
Nine green bottles hanging on the wall,
15+
And if one green bottle should accidentally fall,
16+
There'll be eight green bottles hanging on the wall.
17+
18+
Eight green bottles hanging on the wall,
19+
Eight green bottles hanging on the wall,
20+
And if one green bottle should accidentally fall,
21+
There'll be seven green bottles hanging on the wall.
22+
23+
Seven green bottles hanging on the wall,
24+
Seven green bottles hanging on the wall,
25+
And if one green bottle should accidentally fall,
26+
There'll be six green bottles hanging on the wall.
27+
28+
Six green bottles hanging on the wall,
29+
Six green bottles hanging on the wall,
30+
And if one green bottle should accidentally fall,
31+
There'll be five green bottles hanging on the wall.
32+
33+
Five green bottles hanging on the wall,
34+
Five green bottles hanging on the wall,
35+
And if one green bottle should accidentally fall,
36+
There'll be four green bottles hanging on the wall.
37+
38+
Four green bottles hanging on the wall,
39+
Four green bottles hanging on the wall,
40+
And if one green bottle should accidentally fall,
41+
There'll be three green bottles hanging on the wall.
42+
43+
Three green bottles hanging on the wall,
44+
Three green bottles hanging on the wall,
45+
And if one green bottle should accidentally fall,
46+
There'll be two green bottles hanging on the wall.
47+
48+
Two green bottles hanging on the wall,
49+
Two green bottles hanging on the wall,
50+
And if one green bottle should accidentally fall,
51+
There'll be one green bottle hanging on the wall.
52+
53+
One green bottle hanging on the wall,
54+
One green bottle hanging on the wall,
55+
And if one green bottle should accidentally fall,
56+
There'll be no green bottles hanging on the wall.
57+
```
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+
"bottle-song.sql"
8+
],
9+
"test": [
10+
"bottle-song_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
14+
]
15+
},
16+
"blurb": "Produce the lyrics to the popular children's repetitive song: Ten Green Bottles.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Ten_Green_Bottles"
19+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
DROP TABLE IF EXISTS pairs;
2+
CREATE TEMPORARY TABLE pairs (
3+
verse_number INTEGER NOT NULL,
4+
start_number TEXT NOT NULL,
5+
next_number TEXT NOT NULL
6+
);
7+
INSERT INTO pairs (verse_number, start_number, next_number)
8+
VALUES (10, 'Ten' , 'Nine' ),
9+
( 9, 'Nine' , 'Eight'),
10+
( 8, 'Eight', 'Seven'),
11+
( 7, 'Seven', 'Six' ),
12+
( 6, 'Six' , 'Five' ),
13+
( 5, 'Five' , 'Four' ),
14+
( 4, 'Four' , 'Three'),
15+
( 3, 'Three', 'Two' ),
16+
( 2, 'Two' , 'One' ),
17+
( 1, 'One' , 'no' );
18+
19+
DROP TABLE IF EXISTS verses;
20+
CREATE TABLE verses AS
21+
SELECT verse_number,
22+
PRINTF('%s green bottles hanging on the wall,' || CHAR(10), start_number) ||
23+
PRINTF('%s green bottles hanging on the wall,' || CHAR(10), start_number) ||
24+
'And if one green bottle should accidentally fall,' || CHAR(10) ||
25+
PRINTF('There''ll be %s green bottles hanging on the wall.',LOWER(next_number)) AS verse
26+
FROM pairs
27+
;
28+
29+
UPDATE verses
30+
SET verse = REPLACE(verse, 'be one green bottles', 'be one green bottle')
31+
WHERE verse_number = 2;
32+
UPDATE verses
33+
SET verse = REPLACE(verse, 'One green bottles', 'One green bottle')
34+
WHERE verse_number = 1;
35+
36+
37+
UPDATE "bottle-song"
38+
SET result = (
39+
SELECT GROUP_CONCAT(verse, CHAR(10)||CHAR(10))
40+
FROM (
41+
SELECT verse
42+
FROM verses
43+
WHERE verse_number <= start_bottles
44+
AND verse_number > start_bottles - take_down
45+
ORDER BY verse_number DESC
46+
)
47+
)
48+
;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
[d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03]
13+
description = "verse -> single verse -> first generic verse"
14+
15+
[0f0aded3-472a-4c64-b842-18d4f1f5f030]
16+
description = "verse -> single verse -> last generic verse"
17+
18+
[f61f3c97-131f-459e-b40a-7428f3ed99d9]
19+
description = "verse -> single verse -> verse with 2 bottles"
20+
21+
[05eadba9-5dbd-401e-a7e8-d17cc9baa8e0]
22+
description = "verse -> single verse -> verse with 1 bottle"
23+
24+
[a4a28170-83d6-4dc1-bd8b-319b6abb6a80]
25+
description = "lyrics -> multiple verses -> first two verses"
26+
27+
[3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db]
28+
description = "lyrics -> multiple verses -> last three verses"
29+
30+
[28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28]
31+
description = "lyrics -> multiple verses -> all verses"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Schema:
2+
-- CREATE TABLE "bottle-song" (
3+
-- start_bottles INTEGER NOT NULL,
4+
-- take_down INTEGER NOT NULL,
5+
-- result TEXT
6+
-- );
7+
-- Task: update bottle-song table and set the result based on the
8+
-- start_bottles and take_down.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 ./bottle-song.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_bottles, take_down, result FROM "bottle-song") AS actual
17+
WHERE (actual.start_bottles, actual.take_down, actual.result) = (tests.start_bottles, tests.take_down, tests.expected);
18+
19+
-- Update message for failed tests to give helpful information:
20+
UPDATE tests
21+
SET message = (
22+
'Result for "'
23+
|| PRINTF('start_bottles=%d, take_down=%d', tests.start_bottles,tests.take_down)
24+
|| '"'
25+
|| ' is <' || COALESCE(actual.result, 'NULL')
26+
|| '> but should be <' || tests.expected || '>'
27+
)
28+
FROM (SELECT start_bottles, take_down, result FROM "bottle-song") AS actual
29+
WHERE (actual.start_bottles, actual.take_down) = (tests.start_bottles, tests.take_down) AND tests.status = 'fail';
30+
31+
-- Save results to ./output.json (needed by the online test-runner)
32+
.mode json
33+
.once './output.json'
34+
SELECT description, status, message, output, test_code, task_id
35+
FROM tests;
36+
37+
-- Display test results in readable form for the student:
38+
.mode table
39+
SELECT description, status, message
40+
FROM tests;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DROP TABLE IF EXISTS "bottle-song";
2+
CREATE TABLE "bottle-song" (
3+
start_bottles INTEGER NOT NULL,
4+
take_down INTEGER NOT NULL,
5+
result TEXT
6+
);
7+
8+
.mode csv
9+
.import ./data.csv "bottle-song"
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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_bottles INTEGER NOT NULL,
14+
take_down INTEGER NOT NULL,
15+
expected TEXT NOT NULL
16+
);
17+
18+
INSERT INTO tests (uuid, description, start_bottles, take_down, expected)
19+
VALUES
20+
('d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03','first generic verse',10,1,'Ten green bottles hanging on the wall,
21+
Ten green bottles hanging on the wall,
22+
And if one green bottle should accidentally fall,
23+
There''ll be nine green bottles hanging on the wall.'),
24+
('0f0aded3-472a-4c64-b842-18d4f1f5f030','last generic verse',3,1,'Three green bottles hanging on the wall,
25+
Three green bottles hanging on the wall,
26+
And if one green bottle should accidentally fall,
27+
There''ll be two green bottles hanging on the wall.'),
28+
('f61f3c97-131f-459e-b40a-7428f3ed99d9','verse with 2 bottles',2,1,'Two green bottles hanging on the wall,
29+
Two green bottles hanging on the wall,
30+
And if one green bottle should accidentally fall,
31+
There''ll be one green bottle hanging on the wall.'),
32+
('05eadba9-5dbd-401e-a7e8-d17cc9baa8e0','verse with 1 bottle',1,1,'One green bottle hanging on the wall,
33+
One green bottle hanging on the wall,
34+
And if one green bottle should accidentally fall,
35+
There''ll be no green bottles hanging on the wall.'),
36+
('a4a28170-83d6-4dc1-bd8b-319b6abb6a80','first two verses',10,2,'Ten green bottles hanging on the wall,
37+
Ten green bottles hanging on the wall,
38+
And if one green bottle should accidentally fall,
39+
There''ll be nine green bottles hanging on the wall.
40+
41+
Nine green bottles hanging on the wall,
42+
Nine green bottles hanging on the wall,
43+
And if one green bottle should accidentally fall,
44+
There''ll be eight green bottles hanging on the wall.'),
45+
('3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db','last three verses',3,3,'Three green bottles hanging on the wall,
46+
Three green bottles hanging on the wall,
47+
And if one green bottle should accidentally fall,
48+
There''ll be two green bottles hanging on the wall.
49+
50+
Two green bottles hanging on the wall,
51+
Two green bottles hanging on the wall,
52+
And if one green bottle should accidentally fall,
53+
There''ll be one green bottle hanging on the wall.
54+
55+
One green bottle hanging on the wall,
56+
One green bottle hanging on the wall,
57+
And if one green bottle should accidentally fall,
58+
There''ll be no green bottles hanging on the wall.'),
59+
('28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28','all verses',10,10,'Ten green bottles hanging on the wall,
60+
Ten green bottles hanging on the wall,
61+
And if one green bottle should accidentally fall,
62+
There''ll be nine green bottles hanging on the wall.
63+
64+
Nine green bottles hanging on the wall,
65+
Nine green bottles hanging on the wall,
66+
And if one green bottle should accidentally fall,
67+
There''ll be eight green bottles hanging on the wall.
68+
69+
Eight green bottles hanging on the wall,
70+
Eight green bottles hanging on the wall,
71+
And if one green bottle should accidentally fall,
72+
There''ll be seven green bottles hanging on the wall.
73+
74+
Seven green bottles hanging on the wall,
75+
Seven green bottles hanging on the wall,
76+
And if one green bottle should accidentally fall,
77+
There''ll be six green bottles hanging on the wall.
78+
79+
Six green bottles hanging on the wall,
80+
Six green bottles hanging on the wall,
81+
And if one green bottle should accidentally fall,
82+
There''ll be five green bottles hanging on the wall.
83+
84+
Five green bottles hanging on the wall,
85+
Five green bottles hanging on the wall,
86+
And if one green bottle should accidentally fall,
87+
There''ll be four green bottles hanging on the wall.
88+
89+
Four green bottles hanging on the wall,
90+
Four green bottles hanging on the wall,
91+
And if one green bottle should accidentally fall,
92+
There''ll be three green bottles hanging on the wall.
93+
94+
Three green bottles hanging on the wall,
95+
Three green bottles hanging on the wall,
96+
And if one green bottle should accidentally fall,
97+
There''ll be two green bottles hanging on the wall.
98+
99+
Two green bottles hanging on the wall,
100+
Two green bottles hanging on the wall,
101+
And if one green bottle should accidentally fall,
102+
There''ll be one green bottle hanging on the wall.
103+
104+
One green bottle hanging on the wall,
105+
One green bottle hanging on the wall,
106+
And if one green bottle should accidentally fall,
107+
There''ll be no green bottles hanging on the wall.');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
10,1,""
2+
3,1,""
3+
2,1,""
4+
1,1,""
5+
10,2,""
6+
3,3,""
7+
10,10,""

0 commit comments

Comments
 (0)