Published at Jul 13 2018
·
6 comments

Instructions

Test suite

Solution

Given a string containing brackets `[]`

, braces `{}`

and parentheses `()`

,
verify that all the pairs are matched and nested correctly.

To run the tests, run the command `busted`

from within the exercise directory.

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Ginna Baker

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
local brackets = require('bracket-push')
describe('bracket-push', function()
it('should accept an empty string', function()
assert.is_true(brackets.valid(''))
end)
it('should accept a matched pair of braces', function()
assert.is_true(brackets.valid('{}'))
end)
it('should reject an opening brace without a closing brace', function()
assert.is_false(brackets.valid('{'))
end)
it('should reject a closing brace without an opening brace', function()
assert.is_false(brackets.valid('}{'))
end)
it('should accept a matched pair of parens', function()
assert.is_true(brackets.valid('()'))
end)
it('should reject an opening paren without a closing paren', function()
assert.is_false(brackets.valid('('))
end)
it('should reject a closing paren without an opening paren', function()
assert.is_false(brackets.valid(')('))
end)
it('should accept a matched pair of square brackets', function()
assert.is_true(brackets.valid('[]'))
end)
it('should reject an opening square bracket without a closing square bracket', function()
assert.is_false(brackets.valid('['))
end)
it('should reject a closing square bracket without an opening square bracket', function()
assert.is_false(brackets.valid(']['))
end)
it('should accept more than one pair of brackets', function()
assert.is_true(brackets.valid('[]{}()'))
end)
it('should accept nested brackets', function()
assert.is_true(brackets.valid('[{}]'))
end)
it('should reject properly balanced but improperly nested brackets', function()
assert.is_false(brackets.valid('[{]}'))
end)
it('should allow deep nesting of brackets', function()
assert.is_true(brackets.valid('[{(([{}]))}]'))
end)
it('should accept complicated brackets that are balanced and properly nested', function()
assert.is_true(brackets.valid('{[]([()])}'))
end)
it('should reject complicated brackets that are not balanced', function()
assert.is_false(brackets.valid('{[]([()]}'))
end)
it('should reject complicated brackets that are not properly nested', function()
assert.is_false(brackets.valid('{[]([()]})'))
end)
it('should ignore non-bracket characters', function()
assert.is_true(brackets.valid('{hello[]([a()])b}c'))
end)
end)
```

```
local function valid(brackets)
local b = {}
local bracket_matches = false
for i = 1, #brackets do
local bracket = brackets:sub(i,i)
if bracket == '{' or bracket == '(' or bracket == '[' then table.insert(b , #b + 1, bracket)
elseif bracket == '}' then
if b[#b] == '{' then table.remove(b,#b)
else return false end
elseif bracket == ')' then
if b[#b] == '(' then table.remove(b,#b)
else return false end
elseif bracket == ']' then
if b[#b] == '[' then table.remove(b,#b)
else return false end
end
end
if #b == 0 then bracket_matches = true end
return bracket_matches
end
return {
valid = valid
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,276 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

I tried to reference other guys solution after submit this.. but SevenRoot is the only one I can find.. when we fetch new problem.. exercism fetches new problem randomly? I can see lots of other problems (that I have not fetched) already solved from other guys (it's locked so I could not see their solution).

It always pulls the problems in the same order, but lots of new problems are being added so it seems out of order. I will put my solution up :)

You can simplify your table insertion from table.insert(b , #b + 1, bracket) to table.insert(b , bracket) (if you do not give an index it will insert at the end).

One thing that you can do to simplify is to modify your large if-elseif-elseif-elseif-end to use a table that maps closing brackets to opening brackets instead of lots of conditionals.

@ryanplusplus yeah I saw your solution. It's very neat~! It shows 'power of table' in lua.~!

looks familiar :)