ruby - bracket-push
This commit is contained in:
parent
166d394ed3
commit
4c861d0858
35
ruby/bracket-push/README.md
Normal file
35
ruby/bracket-push/README.md
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# Bracket Push
|
||||||
|
|
||||||
|
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](http://exercism.io/languages/ruby/resources).
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
Ginna Baker
|
||||||
|
|
||||||
|
## Submitting Incomplete Solutions
|
||||||
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
13
ruby/bracket-push/bracket_push.rb
Normal file
13
ruby/bracket-push/bracket_push.rb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# Brackets class
|
||||||
|
class Brackets
|
||||||
|
PAIRS = { '[' => ']', '{' => '}', '(' => ')' }.freeze
|
||||||
|
|
||||||
|
def self.paired?(text)
|
||||||
|
stack = []
|
||||||
|
text.chars.each do |char|
|
||||||
|
stack.push(char) if PAIRS.key?(char)
|
||||||
|
return false if PAIRS.key(char) && PAIRS.key(char) != stack.pop
|
||||||
|
end
|
||||||
|
stack.empty?
|
||||||
|
end
|
||||||
|
end
|
87
ruby/bracket-push/bracket_push_test.rb
Normal file
87
ruby/bracket-push/bracket_push_test.rb
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
require 'minitest/autorun'
|
||||||
|
require_relative 'bracket_push'
|
||||||
|
|
||||||
|
# Common test data version: 1.4.0 602c610
|
||||||
|
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_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
|
Loading…
x
Reference in New Issue
Block a user