Skip to content

Instantly share code, notes, and snippets.

@dialupdev
Last active March 9, 2016 19:21
Show Gist options
  • Save dialupdev/638379b236360d115a26 to your computer and use it in GitHub Desktop.
Save dialupdev/638379b236360d115a26 to your computer and use it in GitHub Desktop.
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