Skip to content

Instantly share code, notes, and snippets.

@dylanerichards
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save dylanerichards/c52ceaf7df8ddeb2d7bb to your computer and use it in GitHub Desktop.

Select an option

Save dylanerichards/c52ceaf7df8ddeb2d7bb to your computer and use it in GitHub Desktop.
Parentheses Checker
class ParenthesesChecker
attr_reader :input
def initialize(input)
@input = input
end
def check_valid_parentheses
if begins_incorrectly? || ends_incorrectly? || unmatching_paren_count?
false
else
true
end
end
private
def begins_incorrectly?
input[0] == ")"
end
def ends_incorrectly?
input[-1] == "("
end
def unmatching_paren_count?
input.scan(")").count != input.scan("(").count
end
end
require_relative '../lib/parentheses'
describe ParenthesesChecker do
describe "#check_valid_parentheses" do
it "returns false if the passed in string begins with )" do
parentheses_checker = ParenthesesChecker.new(")())")
expect(parentheses_checker.check_valid_parentheses).to eq false
end
it "returns false if the passed in string ends with (" do
parentheses_checker = ParenthesesChecker.new("((((")
expect(parentheses_checker.check_valid_parentheses).to eq false
end
it "returns false if the number of ) is not equal to the number of (" do
parentheses_checker = ParenthesesChecker.new("(()")
expect(parentheses_checker.check_valid_parentheses).to eq false
end
it "returns true when valid" do
parentheses_checker = ParenthesesChecker.new("(())")
expect(parentheses_checker.check_valid_parentheses).to eq true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment