Created
January 5, 2012 20:22
-
-
Save bhserna/1567081 to your computer and use it in GitHub Desktop.
Nesting
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 "rspec" | |
def nesting(string) | |
opens = 0 | |
string.each_char do |c| | |
case c | |
when "(" then opens += 1 | |
when ")" then opens > 0 ? (opens -= 1) : (return 0) | |
end | |
end | |
opens == 0 ? 1 : 0 | |
end | |
describe "nesting" do | |
before do | |
@a = "(()(())sdf())()()()((((abc))))(()())((lkjsdf()()()))(()()(()))" | |
@b = ")(()(())())()()()(((())))(()())((()()()))(()()(()))(" | |
end | |
it "should be 1 with a" do | |
nesting(@a).should eq 1 | |
end | |
it "should be 0 with b" do | |
nesting(@b).should eq 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment