Skip to content

Instantly share code, notes, and snippets.

@abonec
Created March 10, 2016 17:28
Show Gist options
  • Save abonec/09e855eda266bf030e62 to your computer and use it in GitHub Desktop.
Save abonec/09e855eda266bf030e62 to your computer and use it in GitHub Desktop.
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