ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# JaeHyoLee's solution

## to Matching Brackets in the Lua Track

Published at Jul 13 2018 · 6 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a string containing brackets [], braces {}, parentheses (), or any combination thereof, verify that any and all pairs are matched and nested correctly.

## Running the tests

To run the tests, run the command busted from within the exercise directory.

## Further information

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

## Submitting Incomplete Solutions

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

### matching-brackets_spec.lua

local brackets = require('matching-brackets')

describe('matching-brackets', 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
}

Solution Author

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.

Solution Author