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

Published at Jul 13 2018
·
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 matching_brackets_test.rb
```

To include color from the command line:

```
ruby -r minitest/pride matching_brackets_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 'matching_brackets'
# Common test data version: 1.5.0 20dd164
class MatchingBracketsTest < 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
```

```
module Brackets
def self.paired?(string)
stack = []
string.each_char do |c|
case c
when '(', '{', '['
stack.push c
when ')'
return false unless '(' == stack.pop
when '}'
return false unless '{' == stack.pop
when ']'
return false unless '[' == stack.pop
else
# Ignore
end
end
stack.empty?
end
end
module BookKeeping
VERSION = 3
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,394 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments