Last active
August 29, 2015 14:14
-
-
Save dylanerichards/c52ceaf7df8ddeb2d7bb to your computer and use it in GitHub Desktop.
Parentheses Checker
This file contains hidden or 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 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 |
This file contains hidden or 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_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