Published at Feb 17 2019
·
0 comments

Instructions

Test suite

Solution

Given a string containing brackets `[]`

, braces `{}`

, parentheses `()`

,
or any combination thereof, verify that any and all pairs are matched
and nested correctly.

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

```
gem install minitest
```

If you would like color output, you can `require 'minitest/pride'`

in
the test file, or note the alternative instruction, below, for running
the test file.

Run the tests from the exercise directory using the following command:

```
ruby bracket_push_test.rb
```

To include color from the command line:

```
ruby -r minitest/pride bracket_push_test.rb
```

Ginna Baker

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

```
require 'minitest/autorun'
require_relative 'bracket_push'
# Common test data version: 1.5.0 20dd164
class BracketPushTest < Minitest::Test
def test_paired_square_brackets
# skip
assert Brackets.paired?('[]')
end
def test_empty_string
skip
assert Brackets.paired?('')
end
def test_unpaired_brackets
skip
refute Brackets.paired?('[[')
end
def test_wrong_ordered_brackets
skip
refute Brackets.paired?('}{')
end
def test_wrong_closing_bracket
skip
refute Brackets.paired?('{]')
end
def test_paired_with_whitespace
skip
assert Brackets.paired?('{ }')
end
def test_partially_paired_brackets
skip
refute Brackets.paired?('{[])')
end
def test_simple_nested_brackets
skip
assert Brackets.paired?('{[]}')
end
def test_several_paired_brackets
skip
assert Brackets.paired?('{}[]')
end
def test_paired_and_nested_brackets
skip
assert Brackets.paired?('([{}({}[])])')
end
def test_unopened_closing_brackets
skip
refute Brackets.paired?('{[)][]}')
end
def test_unpaired_and_nested_brackets
skip
refute Brackets.paired?('([{])')
end
def test_paired_and_wrong_nested_brackets
skip
refute Brackets.paired?('[({]})')
end
def test_paired_and_incomplete_brackets
skip
refute Brackets.paired?('{}[')
end
def test_too_many_closing_brackets
skip
refute Brackets.paired?('[]]')
end
def test_math_expression
skip
assert Brackets.paired?('(((185 + 223.85) * 15) - 543)/2')
end
def test_complex_latex_expression
skip
string = '\left(\begin{array}{cc} \frac{1}{3} & x\\ ' +
'\mathrm{e}^{x} &... x^2 \end{array}\right)'
assert Brackets.paired?(string)
end
end
```

```
class Brackets
OPEN_TO_CLOSE = {
"{" => "}",
"(" => ")",
"[" => "]",
}
def self.paired?(input)
bracket_stack = []
input.each_char do |bracket|
if OPEN_TO_CLOSE.has_key?(bracket)
bracket_stack << bracket
elsif OPEN_TO_CLOSE.has_value?(bracket)
if OPEN_TO_CLOSE[bracket_stack.pop] != bracket
return false
end
end
end
bracket_stack.empty?
end
end
```

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,089 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments