Skip to content

Commit 72511de

Browse files
committed
Add matching-brackets
1 parent e5e3885 commit 72511de

File tree

8 files changed

+206
-0
lines changed

8 files changed

+206
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,14 @@
596596
"prerequisites": [],
597597
"difficulty": 4
598598
},
599+
{
600+
"slug": "matching-brackets",
601+
"name": "Matching Brackets",
602+
"uuid": "c206ca0e-4810-44c5-b519-9a0c660903a0",
603+
"practices": [],
604+
"prerequisites": [],
605+
"difficulty": 5
606+
},
599607
{
600608
"slug": "nucleotide-count",
601609
"name": "Nucleotide Count",
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+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"matching_brackets.vim"
8+
],
9+
"test": [
10+
"matching_brackets.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
14+
]
15+
},
16+
"blurb": "Make sure the brackets and braces all match.",
17+
"source": "Ginna Baker"
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function! IsPaired(str) abort
2+
let l:stack = []
3+
let l:pairs = {'(': ')', '[': ']', '{': '}'}
4+
5+
for l:char in split(a:str, '\zs')
6+
if has_key(l:pairs, l:char)
7+
call add(l:stack, l:char)
8+
elseif index(values(l:pairs), l:char) != -1
9+
if empty(l:stack)
10+
return 0
11+
endif
12+
13+
let l:popped = remove(l:stack, -1)
14+
if l:pairs[popped] !=# l:char
15+
return 0
16+
endif
17+
endif
18+
endfor
19+
20+
return empty(l:stack)
21+
endfunction
22+
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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
Execute (paired square brackets):
3+
Assert IsPaired("[]")
4+
5+
Execute (empty string):
6+
Assert IsPaired("")
7+
8+
Execute (unpaired brackets):
9+
Assert !IsPaired("[[")
10+
11+
Execute (wrong ordered brackets):
12+
Assert !IsPaired("}{")
13+
14+
Execute (wrong closing bracket):
15+
Assert !IsPaired("{]")
16+
17+
Execute (paired with whitespace):
18+
Assert IsPaired("{ }")
19+
20+
Execute (partially paired brackets):
21+
Assert !IsPaired("{[])")
22+
23+
Execute (simple nested brackets):
24+
Assert IsPaired("{[]}")
25+
26+
Execute (several paired brackets):
27+
Assert IsPaired("{}[]")
28+
29+
Execute (paired and nested brackets):
30+
Assert IsPaired("([{}({}[])])")
31+
32+
Execute (unopened closing brackets):
33+
Assert !IsPaired("{[)][]}")
34+
35+
Execute (unpaired and nested brackets):
36+
Assert !IsPaired("([{])")
37+
38+
Execute (paired and wrong nested brackets):
39+
Assert !IsPaired("[({]})")
40+
41+
Execute (paired and wrong nested brackets but innermost are correct):
42+
Assert !IsPaired("[({}])")
43+
44+
Execute (paired and incomplete brackets):
45+
Assert !IsPaired("{}[")
46+
47+
Execute (too many closing brackets):
48+
Assert !IsPaired("[]]")
49+
50+
Execute (early unexpected brackets):
51+
Assert !IsPaired(")()")
52+
53+
Execute (early mismatched brackets):
54+
Assert !IsPaired("{)()")
55+
56+
Execute (math expression):
57+
Assert IsPaired("(((185 + 223.85) * 15) - 543)/2")
58+
59+
Execute (complex latex expression):
60+
let g:value = "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)"
61+
Assert IsPaired(g:value)
62+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
" Given a string containing brackets, determine if the brackets are balanced.
2+
"
3+
" Example:
4+
"
5+
" :echo IsPaired("()[]{}")
6+
" 1
7+
" :echo IsPaired("[[")
8+
" 0
9+
"
10+
function! IsPaired(str) abort
11+
" your implemention goes here
12+
endfunction
13+

0 commit comments

Comments
 (0)