Created
March 10, 2016 17:28
-
-
Save abonec/09e855eda266bf030e62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
module BracketTester | |
module_function | |
def balanced?(str) | |
brackets = 0 | |
str.each_char do |char| | |
if char == '(' | |
brackets += 1 | |
else | |
brackets > 0 ? brackets -= 1 : (return false) | |
end | |
end | |
brackets == 0 | |
end | |
end | |
class TestBracketTester < Minitest::Unit::TestCase | |
def test_balanced | |
assert_equal true, BracketTester.balanced?('()') | |
assert_equal true, BracketTester.balanced?('(())') | |
assert_equal true, BracketTester.balanced?('()()') | |
assert_equal true, BracketTester.balanced?('(()())') | |
assert_equal false, BracketTester.balanced?(')(') | |
assert_equal false, BracketTester.balanced?('()())') | |
assert_equal false, BracketTester.balanced?('(()()') | |
assert_equal false, BracketTester.balanced?('((())))') | |
assert_equal false, BracketTester.balanced?('((()(') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment