Picolisp has a feature called "super parentheses":
Brackets ('[' and ']') can be used as super parentheses. A closing bracket will match [all parentheses to and including] the innermost opening bracket, or [if there are no unmatched opening brackets] all currently open parentheses. src
Taking a string containing only the characters []()
(and newline if your language requires it), remove all closing brackets []
and replace them with the corresponding parentheses. A [
is replaced with a (
, but your program will not have to handle a [
or (
without a matching ]
or )
. Output may contain trailing newlines.
]
matches as many open parens as possible until it hits a [
at which point it has to stop --DJMcMayhem
Examples
((((] -> (((())))
(([()(()])) -> (((()(()))))
) or ( or ] or [) or ([(] -> invalid, do not need to handle
(] -> ()
((](] -> (())()
[] -> ()
[(] -> (())
[[()(]] -> ((()()))
((][(] -> (())(())
(([((](] -> ((((()))()))
This is code-golf, shortest code per language wins.
]
s expand to multiple)
s? \$\endgroup\$(([((]
? \$\endgroup\$[(] -> (())
, the closing bracket matches an opening bracket AND opening parentheses. \$\endgroup\$