Skip to content

Instantly share code, notes, and snippets.

@cored
Last active May 24, 2018 13:53
Show Gist options
  • Save cored/950b71fffb971900dcb67f6243f6b832 to your computer and use it in GitHub Desktop.
Save cored/950b71fffb971900dcb67f6243f6b832 to your computer and use it in GitHub Desktop.
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