Balanced Paren Clusters
Given a string of balanced parentheses, split it up into top-level "clusters".
Examples
(clusters "") ;=> []
(clusters "()") ;=> ["()"]
(clusters "(())") ;=> ["(())"]
(clusters "()()()") ;=> ["()" "()" "()"]
(clusters "(()())()(())") ;=> ["(()())" "()" "(())"]
You can assume the strings will only contain \(
and \)
and will be fully balanced.
Bonuses
- Handle unbalanced parens with a nice error message showing the relevant positions.
- Handle other braces, such as
{}
and[]
.
Thanks to this site for the problem idea, where it is rated Very Hard in Python. The problem has been modified.
Please submit your solutions as comments on this gist.
To subscribe: https://purelyfunctional.tv/newsletter/