Skip to content
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

Add Pascal's Triangle exercise #74

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "pascals-triangle",
"name": "Pascal's Triangle",
"uuid": "fb614e0d-ad10-4710-8785-5ab07fd9eb5a",
"practices": [],
"prerequisites": [],
"difficulty": 8
}
]
},
Expand Down
14 changes: 14 additions & 0 deletions exercises/practice/pascals-triangle/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Instructions

Compute Pascal's triangle up to a given number of rows.

In Pascal's Triangle each number is computed by adding the numbers to the right and left of the current position in the previous row.

```text
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
# ... etc
```
22 changes: 22 additions & 0 deletions exercises/practice/pascals-triangle/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"Steffan153"
],
"files": {
"solution": [
"pascals-triangle.sql"
],
"test": [
"pascals-triangle_test.sql"
],
"example": [
".meta/example.sql"
],
"editor": [
"data.csv"
]
},
"blurb": "Compute Pascal's triangle up to a given number of rows.",
"source": "Pascal's Triangle at Wolfram Math World",
"source_url": "https://www.wolframalpha.com/input/?i=Pascal%27s+triangle"
}
23 changes: 23 additions & 0 deletions exercises/practice/pascals-triangle/.meta/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
UPDATE "pascals-triangle"
SET result = (
WITH RECURSIVE counter(k) AS (
SELECT 1 UNION ALL
SELECT k + 1 FROM counter WHERE k < input
)

SELECT group_concat(
(
WITH RECURSIVE nums(idx, s) AS (
SELECT 1, 1 UNION ALL
SELECT idx + 1, s * k / idx - s FROM nums WHERE idx < k
)

SELECT group_concat(s, ' ') FROM nums
),
char(10) -- newline
) FROM counter
);

UPDATE "pascals-triangle"
SET result = ''
WHERE input = 0;
34 changes: 34 additions & 0 deletions exercises/practice/pascals-triangle/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.

[9920ce55-9629-46d5-85d6-4201f4a4234d]
description = "zero rows"

[70d643ce-a46d-4e93-af58-12d88dd01f21]
description = "single row"

[a6e5a2a2-fc9a-4b47-9f4f-ed9ad9fbe4bd]
description = "two rows"

[97206a99-79ba-4b04-b1c5-3c0fa1e16925]
description = "three rows"

[565a0431-c797-417c-a2c8-2935e01ce306]
description = "four rows"

[06f9ea50-9f51-4eb2-b9a9-c00975686c27]
description = "five rows"

[c3912965-ddb4-46a9-848e-3363e6b00b13]
description = "six rows"

[6cb26c66-7b57-4161-962c-81ec8c99f16b]
description = "ten rows"
8 changes: 8 additions & 0 deletions exercises/practice/pascals-triangle/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DROP TABLE IF EXISTS "pascals-triangle";
CREATE TABLE "pascals-triangle" (
"input" INT,
"result" TEXT
);

.mode csv
.import ./data.csv pascals-triangle
8 changes: 8 additions & 0 deletions exercises/practice/pascals-triangle/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
0,""
1,""
2,""
3,""
4,""
5,""
6,""
10,""
2 changes: 2 additions & 0 deletions exercises/practice/pascals-triangle/pascals-triangle.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Schema: CREATE TABLE "pascals-triangle" ("input" INT, "result" TEXT);
-- Task: update the pascals-triangle table and set the result based on the input field.
48 changes: 48 additions & 0 deletions exercises/practice/pascals-triangle/pascals-triangle_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- Setup test table and read in student solution:
.read ./test_setup.sql

-- Test cases:
INSERT INTO tests (name, uuid,
input, expected)
VALUES
('zero rows', '9920ce55-9629-46d5-85d6-4201f4a4234d', 0, ''),
('single row', '70d643ce-a46d-4e93-af58-12d88dd01f21', 1, '1'),
('two rows', 'a6e5a2a2-fc9a-4b47-9f4f-ed9ad9fbe4bd', 2, '1
1 1'),
('three rows', '97206a99-79ba-4b04-b1c5-3c0fa1e16925', 3, '1
1 1
1 2 1'),
('four rows', '565a0431-c797-417c-a2c8-2935e01ce306', 4, '1
1 1
1 2 1
1 3 3 1'),
('five rows', '06f9ea50-9f51-4eb2-b9a9-c00975686c27', 5, '1
1 1
1 2 1
1 3 3 1
1 4 6 4 1'),
('six rows', 'c3912965-ddb4-46a9-848e-3363e6b00b13', 6, '1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1'),
('ten rows', '6cb26c66-7b57-4161-962c-81ec8c99f16b', 10, '1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1');

-- Comparison of user input and the tests updates the status for each test:
UPDATE tests
SET status = 'pass'
FROM (SELECT input, result FROM "pascals-triangle") AS actual
WHERE (actual.input, actual.result) = (tests.input, tests.expected);

-- Write results and debug info:
.read ./test_reporter.sql
16 changes: 16 additions & 0 deletions exercises/practice/pascals-triangle/test_reporter.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Update message for failed tests to give helpful information:
UPDATE tests
SET message = 'Result for ' || actual.input || ' is: "' || actual.result || '", but should be: "' || tests.expected || '"'
FROM (SELECT input, result FROM "pascals-triangle") AS actual
WHERE actual.input = tests.input AND tests.status = 'fail';

-- Save results to ./output.json (needed by the online test-runner)
.mode json
.once './output.json'
SELECT name, status, message, output, test_code, task_id
FROM tests;

-- Display test results in readable form for the student:
.mode table
SELECT name, status, message
FROM tests;
25 changes: 25 additions & 0 deletions exercises/practice/pascals-triangle/test_setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- 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 ./pascals-triangle.sql
.output

-- Create a clean testing environment:
DROP TABLE IF EXISTS tests;
CREATE TABLE IF NOT EXISTS tests (
-- uuid and name (description) are taken from the test.toml file
uuid TEXT PRIMARY KEY,
name 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
input INT NOT NULL,
expected TEXT NOT NULL
);