Skip to content

Commit 7f3b284

Browse files
authored
matching-brackets (#57)
1 parent 6e9cf34 commit 7f3b284

File tree

10 files changed

+203
-0
lines changed

10 files changed

+203
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,14 @@
438438
"practices": [],
439439
"prerequisites": [],
440440
"difficulty": 4
441+
},
442+
{
443+
"slug": "matching-brackets",
444+
"name": "Matching Brackets",
445+
"uuid": "34ca14e4-31a6-4da6-a5ce-44da523bfb12",
446+
"practices": [],
447+
"prerequisites": [],
448+
"difficulty": 5
441449
}
442450
]
443451
},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"matching_brackets.moon"
8+
],
9+
"test": [
10+
"matching_brackets_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Make sure the brackets and braces all match.",
17+
"source": "Ginna Baker"
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
push = table.insert
2+
pop = table.remove
3+
is_empty = (s) -> #s == 0
4+
5+
opener = { [']']: '[', ['}']: '{', [')']: '(' }
6+
7+
8+
{
9+
is_paired: (input) ->
10+
stack = {}
11+
12+
for char in input\gmatch '.'
13+
switch char
14+
when '[', '{', '(' then push stack, char
15+
when ']', '}', ')' then return false if opener[char] != pop stack
16+
17+
is_empty stack
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
module_imports: {'is_paired'},
3+
generate_test: (case, level) ->
4+
indent "assert.is_#{case.expected} is_paired #{quote case.input.value\gsub('\\', '\\\\')}", level
5+
}
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
is_paired: (input) ->
3+
error 'Implement me'
4+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import is_paired from require 'matching_brackets'
2+
3+
describe 'matching-brackets', ->
4+
it 'paired square brackets', ->
5+
assert.is_true is_paired '[]'
6+
7+
pending 'empty string', ->
8+
assert.is_true is_paired ''
9+
10+
pending 'unpaired brackets', ->
11+
assert.is_false is_paired '[['
12+
13+
pending 'wrong ordered brackets', ->
14+
assert.is_false is_paired '}{'
15+
16+
pending 'wrong closing bracket', ->
17+
assert.is_false is_paired '{]'
18+
19+
pending 'paired with whitespace', ->
20+
assert.is_true is_paired '{ }'
21+
22+
pending 'partially paired brackets', ->
23+
assert.is_false is_paired '{[])'
24+
25+
pending 'simple nested brackets', ->
26+
assert.is_true is_paired '{[]}'
27+
28+
pending 'several paired brackets', ->
29+
assert.is_true is_paired '{}[]'
30+
31+
pending 'paired and nested brackets', ->
32+
assert.is_true is_paired '([{}({}[])])'
33+
34+
pending 'unopened closing brackets', ->
35+
assert.is_false is_paired '{[)][]}'
36+
37+
pending 'unpaired and nested brackets', ->
38+
assert.is_false is_paired '([{])'
39+
40+
pending 'paired and wrong nested brackets', ->
41+
assert.is_false is_paired '[({]})'
42+
43+
pending 'paired and wrong nested brackets but innermost are correct', ->
44+
assert.is_false is_paired '[({}])'
45+
46+
pending 'paired and incomplete brackets', ->
47+
assert.is_false is_paired '{}['
48+
49+
pending 'too many closing brackets', ->
50+
assert.is_false is_paired '[]]'
51+
52+
pending 'early unexpected brackets', ->
53+
assert.is_false is_paired ')()'
54+
55+
pending 'early mismatched brackets', ->
56+
assert.is_false is_paired '{)()'
57+
58+
pending 'math expression', ->
59+
assert.is_true is_paired '(((185 + 223.85) * 15) - 543)/2'
60+
61+
pending 'complex latex expression', ->
62+
assert.is_true is_paired '\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)'

0 commit comments

Comments
 (0)