Skip to content

Instantly share code, notes, and snippets.

@bhserna
Created January 5, 2012 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhserna/1567081 to your computer and use it in GitHub Desktop.
Save bhserna/1567081 to your computer and use it in GitHub Desktop.
Nesting
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