Last active
May 24, 2018 13:53
-
-
Save cored/950b71fffb971900dcb67f6243f6b832 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" | |
require "minitest/pride" | |
module ParenthesisValidator | |
extend self | |
OPENERS = %w|( { [| | |
def call(code) | |
return false if code.size == 1 | |
openers = [] | |
0.upto(code.size-1).each do |idx| | |
if OPENERS.include?(code[idx]) | |
openers << code[idx] | |
else | |
return false if openers.empty? | |
return false if openers.pop == code[idx] | |
end | |
end | |
true | |
end | |
end | |
describe ParenthesisValidator do | |
it "returns true for empty string" do | |
# ParenthesisValidator.("").must_equal true | |
end | |
it "returns false for one opening (" do | |
# ParenthesisValidator.("(").must_equal false | |
end | |
it "returns false for one closing )" do | |
# ParenthesisValidator.(")").must_equal false | |
end | |
it "returns true for open and closing ()" do | |
# ParenthesisValidator.("()").must_equal true | |
end | |
it "returns false for ())" do | |
# ParenthesisValidator.("())").must_equal false | |
ParenthesisValidator.("{[(])}]}").must_equal false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment