Last active
March 9, 2016 19:21
-
-
Save dialupdev/638379b236360d115a26 to your computer and use it in GitHub Desktop.
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
sentence = 'Sometimes (when I nest them (my parentheticals) too much (like this (and this))) they get confusing.' | |
def closing_paren_index( string, opening_paren_index ) | |
depth = 1 | |
partial = string.slice opening_paren_index + 1..-1 | |
partial.each_char.with_index do |char, index| | |
if char == '(' | |
depth += 1 | |
elsif char == ')' | |
then depth -= 1 | |
end | |
if depth == 0 | |
return index + opening_paren_index + 1 | |
end | |
end | |
'No matching parenthesis found.' | |
end | |
# puts closing_paren_index( sentence, 3 ) | |
puts closing_paren_index( sentence, 10 ) | |
# puts closing_paren_index( sentence, 28 ) | |
# puts closing_paren_index( sentence, 57 ) | |
# puts closing_paren_index( sentence, 68 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment