Skip to content

Commit ca9f449

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - matching-brackets (#161)
[no important files changed]
1 parent 978f2d0 commit ca9f449

File tree

5 files changed

+215
-75
lines changed

5 files changed

+215
-75
lines changed

exercises/practice/matching-brackets/.meta/example.sql

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,44 @@ ADD brackets TEXT DEFAULT '';
33

44
PRAGMA recursive_triggers = ON;
55

6-
CREATE TRIGGER IF NOT EXISTS update_backets
7-
AFTER UPDATE
8-
ON "matching-brackets"
9-
WHEN NEW.brackets != OLD.brackets
10-
BEGIN
11-
UPDATE "matching-brackets"
12-
SET brackets = REPLACE(REPLACE(REPLACE(brackets, '()', ''), '[]', ''), '{}', '')
13-
WHERE brackets = NEW.brackets;
6+
CREATE TRIGGER IF NOT EXISTS update_backets AFTER
7+
UPDATE ON "matching-brackets" WHEN NEW.brackets != OLD.brackets BEGIN
8+
UPDATE "matching-brackets"
9+
SET
10+
brackets = REPLACE(
11+
REPLACE(REPLACE(brackets, '()', ''), '[]', ''),
12+
'{}',
13+
''
14+
)
15+
WHERE
16+
brackets = NEW.brackets;
17+
1418
END;
1519

1620
UPDATE "matching-brackets"
17-
SET brackets = (
18-
WITH cnt(n) AS (
19-
SELECT 1
21+
SET
22+
brackets = (
23+
WITH
24+
cnt (n) AS (
25+
SELECT
26+
1
2027
UNION ALL
21-
SELECT n + 1 FROM cnt WHERE n <= LENGTH(input)
22-
)
23-
24-
-- clean the input to only have ()[]{} characters
25-
SELECT group_concat(SUBSTR(input, n, 1), '')
26-
FROM cnt
27-
WHERE '()[]{}' LIKE ('%' || SUBSTR(input, n, 1) || '%')
28-
);
28+
SELECT
29+
n + 1
30+
FROM
31+
cnt
32+
WHERE
33+
n <= LENGTH(input)
34+
)
35+
-- clean the input to only have ()[]{} characters
36+
SELECT
37+
group_concat(SUBSTR(input, n, 1), '')
38+
FROM
39+
cnt
40+
WHERE
41+
'()[]{}' LIKE('%' || SUBSTR(input, n, 1) || '%')
42+
);
2943

3044
UPDATE "matching-brackets"
31-
SET result = (brackets = '');
45+
SET
46+
result = (brackets = '');
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
DROP TABLE IF EXISTS "matching-brackets";
2-
CREATE TABLE "matching-brackets" (
3-
"input" TEXT,
4-
"result" BOOLEAN
5-
);
2+
3+
CREATE TABLE "matching-brackets" ("input" TEXT, "result" BOOLEAN);
64

75
.mode csv
86
.import ./data.csv matching-brackets
Lines changed: 135 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,144 @@
11
-- Setup test table and read in student solution:
22
.read ./test_setup.sql
3-
43
-- Test cases:
5-
INSERT INTO tests (name, uuid, input, expected)
6-
VALUES
7-
('paired square brackets', '81ec11da-38dd-442a-bcf9-3de7754609a5', '[]', 1),
8-
('empty string', '287f0167-ac60-4b64-8452-a0aa8f4e5238', '', 1),
9-
('unpaired brackets', '6c3615a3-df01-4130-a731-8ef5f5d78dac', '[[', 0),
10-
('wrong ordered brackets', '9d414171-9b98-4cac-a4e5-941039a97a77', '}{', 0),
11-
('wrong closing bracket', 'f0f97c94-a149-4736-bc61-f2c5148ffb85', '{]', 0),
12-
('paired with whitespace', '754468e0-4696-4582-a30e-534d47d69756', '{ }', 1),
13-
('partially paired brackets', 'ba84f6ee-8164-434a-9c3e-b02c7f8e8545', '{[])', 0),
14-
('simple nested brackets', '3c86c897-5ff3-4a2b-ad9b-47ac3a30651d', '{[]}', 1),
15-
('several paired brackets', '2d137f2c-a19e-4993-9830-83967a2d4726', '{}[]', 1),
16-
('paired and nested brackets', '2e1f7b56-c137-4c92-9781-958638885a44', '([{}({}[])])', 1),
17-
('unopened closing brackets', '84f6233b-e0f7-4077-8966-8085d295c19b', '{[)][]}', 0),
18-
('unpaired and nested brackets', '9b18c67d-7595-4982-b2c5-4cb949745d49', '([{])', 0),
19-
('paired and wrong nested brackets', 'a0205e34-c2ac-49e6-a88a-899508d7d68e', '[({]})', 0),
20-
('paired and wrong nested brackets but innermost are correct', '1d5c093f-fc84-41fb-8c2a-e052f9581602', '[({}])', 0),
21-
('paired and incomplete brackets', 'ef47c21b-bcfd-4998-844c-7ad5daad90a8', '{}[', 0),
22-
('too many closing brackets', 'a4675a40-a8be-4fc2-bc47-2a282ce6edbe', '[]]', 0),
23-
('early unexpected brackets', 'a345a753-d889-4b7e-99ae-34ac85910d1a', ')()', 0),
24-
('early mismatched brackets', '21f81d61-1608-465a-b850-baa44c5def83', '{)()', 0),
25-
('math expression', '99255f93-261b-4435-a352-02bdecc9bdf2', '(((185 + 223.85) * 15) - 543)/2', 1),
26-
('complex latex expression', '8e357d79-f302-469a-8515-2561877256a1', '\left(\begin{array}{cc} \frac{1}{3} & x\\ \mathrm{e}^{x} &... x^2 \end{array}\right)', 1);
4+
INSERT INTO
5+
tests (name, uuid, input, expected)
6+
VALUES
7+
(
8+
'paired square brackets',
9+
'81ec11da-38dd-442a-bcf9-3de7754609a5',
10+
'[]',
11+
1
12+
),
13+
(
14+
'empty string',
15+
'287f0167-ac60-4b64-8452-a0aa8f4e5238',
16+
'',
17+
1
18+
),
19+
(
20+
'unpaired brackets',
21+
'6c3615a3-df01-4130-a731-8ef5f5d78dac',
22+
'[[',
23+
0
24+
),
25+
(
26+
'wrong ordered brackets',
27+
'9d414171-9b98-4cac-a4e5-941039a97a77',
28+
'}{',
29+
0
30+
),
31+
(
32+
'wrong closing bracket',
33+
'f0f97c94-a149-4736-bc61-f2c5148ffb85',
34+
'{]',
35+
0
36+
),
37+
(
38+
'paired with whitespace',
39+
'754468e0-4696-4582-a30e-534d47d69756',
40+
'{ }',
41+
1
42+
),
43+
(
44+
'partially paired brackets',
45+
'ba84f6ee-8164-434a-9c3e-b02c7f8e8545',
46+
'{[])',
47+
0
48+
),
49+
(
50+
'simple nested brackets',
51+
'3c86c897-5ff3-4a2b-ad9b-47ac3a30651d',
52+
'{[]}',
53+
1
54+
),
55+
(
56+
'several paired brackets',
57+
'2d137f2c-a19e-4993-9830-83967a2d4726',
58+
'{}[]',
59+
1
60+
),
61+
(
62+
'paired and nested brackets',
63+
'2e1f7b56-c137-4c92-9781-958638885a44',
64+
'([{}({}[])])',
65+
1
66+
),
67+
(
68+
'unopened closing brackets',
69+
'84f6233b-e0f7-4077-8966-8085d295c19b',
70+
'{[)][]}',
71+
0
72+
),
73+
(
74+
'unpaired and nested brackets',
75+
'9b18c67d-7595-4982-b2c5-4cb949745d49',
76+
'([{])',
77+
0
78+
),
79+
(
80+
'paired and wrong nested brackets',
81+
'a0205e34-c2ac-49e6-a88a-899508d7d68e',
82+
'[({]})',
83+
0
84+
),
85+
(
86+
'paired and wrong nested brackets but innermost are correct',
87+
'1d5c093f-fc84-41fb-8c2a-e052f9581602',
88+
'[({}])',
89+
0
90+
),
91+
(
92+
'paired and incomplete brackets',
93+
'ef47c21b-bcfd-4998-844c-7ad5daad90a8',
94+
'{}[',
95+
0
96+
),
97+
(
98+
'too many closing brackets',
99+
'a4675a40-a8be-4fc2-bc47-2a282ce6edbe',
100+
'[]]',
101+
0
102+
),
103+
(
104+
'early unexpected brackets',
105+
'a345a753-d889-4b7e-99ae-34ac85910d1a',
106+
')()',
107+
0
108+
),
109+
(
110+
'early mismatched brackets',
111+
'21f81d61-1608-465a-b850-baa44c5def83',
112+
'{)()',
113+
0
114+
),
115+
(
116+
'math expression',
117+
'99255f93-261b-4435-a352-02bdecc9bdf2',
118+
'(((185 + 223.85) * 15) - 543)/2',
119+
1
120+
),
121+
(
122+
'complex latex expression',
123+
'8e357d79-f302-469a-8515-2561877256a1',
124+
'\left(\begin{array}{cc} \frac{1}{3} & x\\ \mathrm{e}^{x} &... x^2 \end{array}\right)',
125+
1
126+
);
27127

28128
-- Comparison of user input and the tests updates the status for each test:
29129
UPDATE tests
30-
SET status = 'pass'
31-
FROM (SELECT input, result FROM "matching-brackets") AS actual
32-
WHERE (actual.input, actual.result) = (tests.input, tests.expected);
130+
SET
131+
status = 'pass'
132+
FROM
133+
(
134+
SELECT
135+
input,
136+
result
137+
FROM
138+
"matching-brackets"
139+
) AS actual
140+
WHERE
141+
(actual.input, actual.result) = (tests.input, tests.expected);
33142

34143
-- Write results and debug info:
35144
.read ./test_reporter.sql
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.input || '"'
5-
|| ' is <' || COALESCE(actual.result, 'NULL')
6-
|| '> but should be <' || tests.expected || '>'
7-
)
8-
FROM (SELECT input, result FROM 'matching-brackets') AS actual
9-
WHERE actual.input = tests.input AND tests.status = 'fail';
3+
SET
4+
message = (
5+
'Result for "' || tests.input || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
6+
)
7+
FROM
8+
(
9+
SELECT
10+
input,
11+
result
12+
FROM
13+
'matching-brackets'
14+
) AS actual
15+
WHERE
16+
actual.input = tests.input
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 ./matching-brackets.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 name (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-
input TEXT NOT NULL,
24-
expected BOOLEAN NOT NULL
12+
-- uuid and name (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+
input TEXT NOT NULL,
23+
expected BOOLEAN NOT NULL
2524
);

0 commit comments

Comments
 (0)