Skip to content

Commit 58108ba

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - collatz-conjecture (#146)
[no important files changed]
1 parent 163835c commit 58108ba

File tree

5 files changed

+101
-56
lines changed

5 files changed

+101
-56
lines changed

exercises/practice/collatz-conjecture/.meta/example.sql

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ ALTER TABLE collatz
22
ADD current INTEGER;
33

44
UPDATE collatz
5-
SET steps = 0;
5+
SET
6+
steps = 0;
67

78
PRAGMA recursive_triggers = ON;
89

9-
CREATE TRIGGER IF NOT EXISTS update_collatz
10-
AFTER UPDATE
11-
ON collatz
12-
WHEN NEW.current != 1
13-
BEGIN
14-
UPDATE collatz
15-
SET current = IIF(NEW.current % 2 == 0, NEW.current / 2, NEW.current * 3 + 1),
16-
steps = NEW.steps + 1
17-
WHERE current = NEW.current;
10+
CREATE TRIGGER IF NOT EXISTS update_collatz AFTER
11+
UPDATE ON collatz WHEN NEW.current != 1 BEGIN
12+
UPDATE collatz
13+
SET
14+
current = IIF(
15+
NEW.current % 2 == 0,
16+
NEW.current / 2,
17+
NEW.current * 3 + 1
18+
),
19+
steps = NEW.steps + 1
20+
WHERE
21+
current = NEW.current;
22+
1823
END;
1924

2025
UPDATE collatz
21-
SET current = number;
26+
SET
27+
current = number;
Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
11
-- Setup test table and read in student solution:
22
.read ./test_setup.sql
3-
43
-- Test cases:
54
-- Note: the strings below may contain literal tab, newline, carriage returns.
6-
7-
INSERT INTO tests (name, uuid, number, expected)
8-
VALUES
9-
('zero steps for one', '540a3d51-e7a6-47a5-92a3-4ad1838f0bfd',
10-
1, 0),
11-
('divide if even', '3d76a0a6-ea84-444a-821a-f7857c2c1859',
12-
16, 4),
13-
('even and odd steps', '754dea81-123c-429e-b8bc-db20b05a87b9',
14-
12, 9),
15-
('large number of even and odd steps', 'ecfd0210-6f85-44f6-8280-f65534892ff6',
16-
1000000, 152);
5+
INSERT INTO
6+
tests (name, uuid, number, expected)
7+
VALUES
8+
(
9+
'zero steps for one',
10+
'540a3d51-e7a6-47a5-92a3-4ad1838f0bfd',
11+
1,
12+
0
13+
),
14+
(
15+
'divide if even',
16+
'3d76a0a6-ea84-444a-821a-f7857c2c1859',
17+
16,
18+
4
19+
),
20+
(
21+
'even and odd steps',
22+
'754dea81-123c-429e-b8bc-db20b05a87b9',
23+
12,
24+
9
25+
),
26+
(
27+
'large number of even and odd steps',
28+
'ecfd0210-6f85-44f6-8280-f65534892ff6',
29+
1000000,
30+
152
31+
);
1732

1833
-- Comparison of user input and the tests updates the status for each test:
1934
UPDATE tests
20-
SET status = 'pass'
21-
FROM (SELECT number, steps FROM collatz) AS actual
22-
WHERE (actual.number, actual.steps) = (tests.number, tests.expected);
35+
SET
36+
status = 'pass'
37+
FROM
38+
(
39+
SELECT
40+
number,
41+
steps
42+
FROM
43+
collatz
44+
) AS actual
45+
WHERE
46+
(actual.number, actual.steps) = (tests.number, tests.expected);
2347

2448
-- Write results and debug info:
2549
.read ./test_reporter.sql
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
DROP TABLE IF EXISTS collatz;
2-
CREATE TABLE "collatz" (
3-
"number" INTEGER,
4-
"steps" INTEGER
5-
);
62

7-
-- Note: the CSV file contain literal tab, newline, carriage returns.
3+
CREATE TABLE "collatz" ("number" INTEGER, "steps" INTEGER);
84

5+
-- Note: the CSV file contain literal tab, newline, carriage returns.
96
.mode csv
107
.import ./data.csv collatz
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
-- Update message for failed tests to give helpful information:
22
UPDATE tests
3-
SET message = (
4-
'Result for ' || tests.number
5-
|| ' is <' || COALESCE(actual.steps, 'NULL')
6-
|| '> but should be <' || tests.expected || '>'
7-
)
8-
FROM (SELECT number, steps FROM collatz) AS actual
9-
WHERE actual.number = tests.number AND tests.status = 'fail';
3+
SET
4+
message = (
5+
'Result for ' || tests.number || ' is <' || COALESCE(actual.steps, 'NULL') || '> but should be <' || tests.expected || '>'
6+
)
7+
FROM
8+
(
9+
SELECT
10+
number,
11+
steps
12+
FROM
13+
collatz
14+
) AS actual
15+
WHERE
16+
actual.number = tests.number
17+
AND tests.status = 'fail';
1018

1119
-- Save results to ./output.json (needed by the online test-runner)
1220
.mode json
1321
.once './output.json'
14-
SELECT name, status, message, output, test_code, task_id
15-
FROM tests;
22+
SELECT
23+
name,
24+
status,
25+
message,
26+
output,
27+
test_code,
28+
task_id
29+
FROM
30+
tests;
1631

1732
-- Display test results in readable form for the student:
1833
.mode table
19-
SELECT name, status, message
20-
FROM tests;
34+
SELECT
35+
name,
36+
status,
37+
message
38+
FROM
39+
tests;
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
-- Create database:
22
.read ./create_fixture.sql
3-
43
-- Read user student solution and save any output as markdown in user_output.md:
54
.mode markdown
65
.output user_output.md
76
.read ./collatz-conjecture.sql
87
.output
9-
108
-- Create a clean testing environment:
119
DROP TABLE IF EXISTS tests;
10+
1211
CREATE TABLE IF NOT EXISTS tests (
13-
-- uuid and description are taken from the test.toml file
14-
uuid TEXT PRIMARY KEY,
15-
name TEXT NOT NULL,
16-
-- The following section is needed by the online test-runner
17-
status TEXT DEFAULT 'fail',
18-
message TEXT,
19-
output TEXT,
20-
test_code TEXT,
21-
task_id INTEGER DEFAULT NULL,
22-
-- Here are columns for the actual tests
23-
number INT NOT NULL,
24-
expected TEXT NOT NULL
12+
-- uuid and description are taken from the test.toml file
13+
uuid TEXT PRIMARY KEY,
14+
name TEXT NOT NULL,
15+
-- The following section is needed by the online test-runner
16+
status TEXT DEFAULT 'fail',
17+
message TEXT,
18+
output TEXT,
19+
test_code TEXT,
20+
task_id INTEGER DEFAULT NULL,
21+
-- Here are columns for the actual tests
22+
number INT NOT NULL,
23+
expected TEXT NOT NULL
2524
);

0 commit comments

Comments
 (0)