So I'm trying to solve a challenge where if the perens are in the right order, I return true; else, of course, false. Code I came up with is here:
def valid_parentheses(str)
return false if str.length % 2 == 1
begin
eval(str)
rescue SyntaxError
false
else
true
end
end
end
Works like a charm except in situations like valid_parentheses("hi(hi)()") which should returns # => true but instead returns false because the ending () are quote-unquote unnecessary and therefore raises an error.
I tried to split it out by parentheses, but:
str.split(/\(.*\))
# =>"hi"
because it deleted all the parens, and:
str.scan(/\(.*\))
#=> "(hi)()"
because it still technically starts with ( and ends with ).
How do I split that up to get "(hi)" and "()" separately?