Skip to content

Commit 627833f

Browse files
authored
feat: add Matching Brackets exercise (#75)
1 parent af5fc20 commit 627833f

File tree

12 files changed

+249
-0
lines changed

12 files changed

+249
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@
202202
"prerequisites": [],
203203
"difficulty": 8
204204
},
205+
{
206+
"slug": "matching-brackets",
207+
"name": "Matching Brackets",
208+
"uuid": "cc2a9d38-4d01-479a-a7d1-f1a5662cbc3e",
209+
"practices": [],
210+
"prerequisites": [],
211+
"difficulty": 8
212+
},
205213
{
206214
"slug": "pascals-triangle",
207215
"name": "Pascal's Triangle",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Instructions
2+
3+
Given a string containing brackets `[]`, braces `{}`, parentheses `()`, or any combination thereof, verify that any and all pairs are matched and nested correctly.
4+
Any other characters should be ignored.
5+
For example, `"{what is (42)}?"` is balanced and `"[text}"` is not.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Introduction
2+
3+
You're given the opportunity to write software for the Bracketeer™, an ancient but powerful mainframe.
4+
The software that runs on it is written in a proprietary language.
5+
Much of its syntax is familiar, but you notice _lots_ of brackets, braces and parentheses.
6+
Despite the Bracketeer™ being powerful, it lacks flexibility.
7+
If the source code has any unbalanced brackets, braces or parentheses, the Bracketeer™ crashes and must be rebooted.
8+
To avoid such a scenario, you start writing code that can verify that brackets, braces, and parentheses are balanced before attempting to run it on the Bracketeer™.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"authors": [
3+
"Steffan153"
4+
],
5+
"files": {
6+
"solution": [
7+
"matching-brackets.sql"
8+
],
9+
"test": [
10+
"matching-brackets_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
14+
],
15+
"editor": [
16+
"data.csv"
17+
]
18+
},
19+
"blurb": "Make sure the brackets and braces all match.",
20+
"source": "Ginna Baker"
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
ALTER TABLE "matching-brackets"
2+
ADD brackets TEXT DEFAULT '';
3+
4+
PRAGMA recursive_triggers = ON;
5+
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;
14+
END;
15+
16+
UPDATE "matching-brackets"
17+
SET brackets = (
18+
WITH cnt(n) AS (
19+
SELECT 1
20+
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+
);
29+
30+
UPDATE "matching-brackets"
31+
SET result = (brackets = '');
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
[81ec11da-38dd-442a-bcf9-3de7754609a5]
13+
description = "paired square brackets"
14+
15+
[287f0167-ac60-4b64-8452-a0aa8f4e5238]
16+
description = "empty string"
17+
18+
[6c3615a3-df01-4130-a731-8ef5f5d78dac]
19+
description = "unpaired brackets"
20+
21+
[9d414171-9b98-4cac-a4e5-941039a97a77]
22+
description = "wrong ordered brackets"
23+
24+
[f0f97c94-a149-4736-bc61-f2c5148ffb85]
25+
description = "wrong closing bracket"
26+
27+
[754468e0-4696-4582-a30e-534d47d69756]
28+
description = "paired with whitespace"
29+
30+
[ba84f6ee-8164-434a-9c3e-b02c7f8e8545]
31+
description = "partially paired brackets"
32+
33+
[3c86c897-5ff3-4a2b-ad9b-47ac3a30651d]
34+
description = "simple nested brackets"
35+
36+
[2d137f2c-a19e-4993-9830-83967a2d4726]
37+
description = "several paired brackets"
38+
39+
[2e1f7b56-c137-4c92-9781-958638885a44]
40+
description = "paired and nested brackets"
41+
42+
[84f6233b-e0f7-4077-8966-8085d295c19b]
43+
description = "unopened closing brackets"
44+
45+
[9b18c67d-7595-4982-b2c5-4cb949745d49]
46+
description = "unpaired and nested brackets"
47+
48+
[a0205e34-c2ac-49e6-a88a-899508d7d68e]
49+
description = "paired and wrong nested brackets"
50+
51+
[1d5c093f-fc84-41fb-8c2a-e052f9581602]
52+
description = "paired and wrong nested brackets but innermost are correct"
53+
54+
[ef47c21b-bcfd-4998-844c-7ad5daad90a8]
55+
description = "paired and incomplete brackets"
56+
57+
[a4675a40-a8be-4fc2-bc47-2a282ce6edbe]
58+
description = "too many closing brackets"
59+
60+
[a345a753-d889-4b7e-99ae-34ac85910d1a]
61+
description = "early unexpected brackets"
62+
63+
[21f81d61-1608-465a-b850-baa44c5def83]
64+
description = "early mismatched brackets"
65+
66+
[99255f93-261b-4435-a352-02bdecc9bdf2]
67+
description = "math expression"
68+
69+
[8e357d79-f302-469a-8515-2561877256a1]
70+
description = "complex latex expression"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
DROP TABLE IF EXISTS "matching-brackets";
2+
CREATE TABLE "matching-brackets" (
3+
"input" TEXT,
4+
"result" BOOLEAN
5+
);
6+
7+
.mode csv
8+
.import ./data.csv matching-brackets
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"[]",
2+
"",
3+
"[[",
4+
"}{",
5+
"{]",
6+
"{ }",
7+
"{[])",
8+
"{[]}",
9+
"{}[]",
10+
"([{}({}[])])",
11+
"{[)][]}",
12+
"([{])",
13+
"[({]})",
14+
"[({}])",
15+
"{}[",
16+
"[]]",
17+
")()",
18+
"{)()",
19+
"(((185 + 223.85) * 15) - 543)/2",
20+
"\left(\begin{array}{cc} \frac{1}{3} & x\\ \mathrm{e}^{x} &... x^2 \end{array}\right)",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Schema: CREATE TABLE "matching-brackets" ("input" TEXT "result" BOOLEAN);
2+
-- Task: update the matching-brackets table and set the result based on the input field.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- Setup test table and read in student solution:
2+
.read ./test_setup.sql
3+
4+
-- 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);
27+
28+
-- Comparison of user input and the tests updates the status for each test:
29+
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);
33+
34+
-- Write results and debug info:
35+
.read ./test_reporter.sql

0 commit comments

Comments
 (0)