Skip to content

Commit af5fc20

Browse files
authored
feat: add Pascal's Triangle exercise (#74)
1 parent 71cb937 commit af5fc20

File tree

11 files changed

+208
-0
lines changed

11 files changed

+208
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@
201201
"practices": [],
202202
"prerequisites": [],
203203
"difficulty": 8
204+
},
205+
{
206+
"slug": "pascals-triangle",
207+
"name": "Pascal's Triangle",
208+
"uuid": "fb614e0d-ad10-4710-8785-5ab07fd9eb5a",
209+
"practices": [],
210+
"prerequisites": [],
211+
"difficulty": 8
204212
}
205213
]
206214
},
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Instructions
2+
3+
Compute Pascal's triangle up to a given number of rows.
4+
5+
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.
6+
7+
```text
8+
1
9+
1 1
10+
1 2 1
11+
1 3 3 1
12+
1 4 6 4 1
13+
# ... etc
14+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"Steffan153"
4+
],
5+
"files": {
6+
"solution": [
7+
"pascals-triangle.sql"
8+
],
9+
"test": [
10+
"pascals-triangle_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
14+
],
15+
"editor": [
16+
"data.csv"
17+
]
18+
},
19+
"blurb": "Compute Pascal's triangle up to a given number of rows.",
20+
"source": "Pascal's Triangle at Wolfram Math World",
21+
"source_url": "https://www.wolframalpha.com/input/?i=Pascal%27s+triangle"
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
UPDATE "pascals-triangle"
2+
SET result = (
3+
WITH RECURSIVE counter(k) AS (
4+
SELECT 1 UNION ALL
5+
SELECT k + 1 FROM counter WHERE k < input
6+
)
7+
8+
SELECT group_concat(
9+
(
10+
WITH RECURSIVE nums(idx, s) AS (
11+
SELECT 1, 1 UNION ALL
12+
SELECT idx + 1, s * k / idx - s FROM nums WHERE idx < k
13+
)
14+
15+
SELECT group_concat(s, ' ') FROM nums
16+
),
17+
char(10) -- newline
18+
) FROM counter
19+
);
20+
21+
UPDATE "pascals-triangle"
22+
SET result = ''
23+
WHERE input = 0;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
[9920ce55-9629-46d5-85d6-4201f4a4234d]
13+
description = "zero rows"
14+
15+
[70d643ce-a46d-4e93-af58-12d88dd01f21]
16+
description = "single row"
17+
18+
[a6e5a2a2-fc9a-4b47-9f4f-ed9ad9fbe4bd]
19+
description = "two rows"
20+
21+
[97206a99-79ba-4b04-b1c5-3c0fa1e16925]
22+
description = "three rows"
23+
24+
[565a0431-c797-417c-a2c8-2935e01ce306]
25+
description = "four rows"
26+
27+
[06f9ea50-9f51-4eb2-b9a9-c00975686c27]
28+
description = "five rows"
29+
30+
[c3912965-ddb4-46a9-848e-3363e6b00b13]
31+
description = "six rows"
32+
33+
[6cb26c66-7b57-4161-962c-81ec8c99f16b]
34+
description = "ten rows"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
DROP TABLE IF EXISTS "pascals-triangle";
2+
CREATE TABLE "pascals-triangle" (
3+
"input" INT,
4+
"result" TEXT
5+
);
6+
7+
.mode csv
8+
.import ./data.csv pascals-triangle
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
0,""
2+
1,""
3+
2,""
4+
3,""
5+
4,""
6+
5,""
7+
6,""
8+
10,""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Schema: CREATE TABLE "pascals-triangle" ("input" INT, "result" TEXT);
2+
-- Task: update the pascals-triangle table and set the result based on the input field.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
-- Setup test table and read in student solution:
2+
.read ./test_setup.sql
3+
4+
-- Test cases:
5+
INSERT INTO tests (name, uuid,
6+
input, expected)
7+
VALUES
8+
('zero rows', '9920ce55-9629-46d5-85d6-4201f4a4234d', 0, ''),
9+
('single row', '70d643ce-a46d-4e93-af58-12d88dd01f21', 1, '1'),
10+
('two rows', 'a6e5a2a2-fc9a-4b47-9f4f-ed9ad9fbe4bd', 2, '1
11+
1 1'),
12+
('three rows', '97206a99-79ba-4b04-b1c5-3c0fa1e16925', 3, '1
13+
1 1
14+
1 2 1'),
15+
('four rows', '565a0431-c797-417c-a2c8-2935e01ce306', 4, '1
16+
1 1
17+
1 2 1
18+
1 3 3 1'),
19+
('five rows', '06f9ea50-9f51-4eb2-b9a9-c00975686c27', 5, '1
20+
1 1
21+
1 2 1
22+
1 3 3 1
23+
1 4 6 4 1'),
24+
('six rows', 'c3912965-ddb4-46a9-848e-3363e6b00b13', 6, '1
25+
1 1
26+
1 2 1
27+
1 3 3 1
28+
1 4 6 4 1
29+
1 5 10 10 5 1'),
30+
('ten rows', '6cb26c66-7b57-4161-962c-81ec8c99f16b', 10, '1
31+
1 1
32+
1 2 1
33+
1 3 3 1
34+
1 4 6 4 1
35+
1 5 10 10 5 1
36+
1 6 15 20 15 6 1
37+
1 7 21 35 35 21 7 1
38+
1 8 28 56 70 56 28 8 1
39+
1 9 36 84 126 126 84 36 9 1');
40+
41+
-- Comparison of user input and the tests updates the status for each test:
42+
UPDATE tests
43+
SET status = 'pass'
44+
FROM (SELECT input, result FROM "pascals-triangle") AS actual
45+
WHERE (actual.input, actual.result) = (tests.input, tests.expected);
46+
47+
-- Write results and debug info:
48+
.read ./test_reporter.sql
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Update message for failed tests to give helpful information:
2+
UPDATE tests
3+
SET message = 'Result for ' || actual.input || ' is: "' || actual.result || '", but should be: "' || tests.expected || '"'
4+
FROM (SELECT input, result FROM "pascals-triangle") AS actual
5+
WHERE actual.input = tests.input AND tests.status = 'fail';
6+
7+
-- Save results to ./output.json (needed by the online test-runner)
8+
.mode json
9+
.once './output.json'
10+
SELECT name, status, message, output, test_code, task_id
11+
FROM tests;
12+
13+
-- Display test results in readable form for the student:
14+
.mode table
15+
SELECT name, status, message
16+
FROM tests;

0 commit comments

Comments
 (0)