From 4c861d08586bc7114144e921a34f5d166eacade6 Mon Sep 17 00:00:00 2001 From: James Walker Date: Mon, 12 Nov 2018 20:10:43 -0500 Subject: [PATCH] ruby - bracket-push --- ruby/bracket-push/README.md | 35 +++++++++++ ruby/bracket-push/bracket_push.rb | 13 ++++ ruby/bracket-push/bracket_push_test.rb | 87 ++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 ruby/bracket-push/README.md create mode 100644 ruby/bracket-push/bracket_push.rb create mode 100644 ruby/bracket-push/bracket_push_test.rb diff --git a/ruby/bracket-push/README.md b/ruby/bracket-push/README.md new file mode 100644 index 0000000..a3affb4 --- /dev/null +++ b/ruby/bracket-push/README.md @@ -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. diff --git a/ruby/bracket-push/bracket_push.rb b/ruby/bracket-push/bracket_push.rb new file mode 100644 index 0000000..1646ca3 --- /dev/null +++ b/ruby/bracket-push/bracket_push.rb @@ -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 diff --git a/ruby/bracket-push/bracket_push_test.rb b/ruby/bracket-push/bracket_push_test.rb new file mode 100644 index 0000000..70582d0 --- /dev/null +++ b/ruby/bracket-push/bracket_push_test.rb @@ -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