Last active
September 26, 2015 15:19
-
-
Save mikhailov/c31607958a4703622217 to your computer and use it in GitHub Desktop.
ValidParentheses
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
class ValidParentheses | |
SYMBOLS = {"(" => ")", "[" => "]", "{" => "}"} | |
def initialize(array) | |
@array = array.split("") | |
@stack = [] | |
end | |
def process | |
@array.each do |i| | |
if SYMBOLS.keys.include?(i) | |
@stack.push(i) | |
elsif SYMBOLS.values.include?(i) | |
if @stack.last == SYMBOLS.key(i) | |
@stack.pop | |
else | |
return false | |
end | |
end | |
end | |
return [email protected]? ? false : true | |
end | |
end | |
require 'minitest/autorun' | |
class TestValidParentheses < Minitest::Unit::TestCase | |
def test_valid_input_1 | |
assert_equal ValidParentheses.new("()").process, true | |
end | |
def test_valid_input_2 | |
assert_equal ValidParentheses.new("()[]{}").process, true | |
end | |
def test_valid_input_3 | |
assert_equal ValidParentheses.new("()([]){[()]}").process, true | |
end | |
def test_valid_input_4 | |
assert_equal ValidParentheses.new("[()[]{}]").process, true | |
end | |
def test_invalid_input_5 | |
assert_equal ValidParentheses.new("(]").process, false | |
end | |
def test_invalid_input_6 | |
assert_equal ValidParentheses.new("([)]").process, false | |
end | |
def test_invalid_input_7 | |
assert_equal ValidParentheses.new("()[)()]").process, false | |
end | |
def test_invalid_input_8 | |
assert_equal ValidParentheses.new("[()[]{}](").process, false | |
end | |
def test_invalid_input_9 | |
assert_equal ValidParentheses.new("[()[]{}]([[[").process, false | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment