Based on THIS question.
Given a string, replace each bracket ()[]{}<>
with a bracket of the appropriate type so that the brackets match, and the nested brackets cycle as follows:
- Outermost ones are
()
- Directly inside of
()
should be[]
- Directly inside of
[]
should be{}
- Directly inside of
{}
should be<>
- Directly inside of
<>
will be()
again (cycles)
All non-bracket chars must remain exactly as they are. Open brackets may only be replaced with open brackets of some type, and close brackets with close brackets.
The input will always make this possible. This means its brackets matched correctly if their type is ignored. So, {ab<)c]
is a valid input, but ab)(cd
or ab((cd
are not.
Examples:
2#jd {¤>. = 2#jd (¤).
abcdef = abcdef
(3×5+(4-1)) = (3×5+[4-1])
<<<>><<>><<<<<<>>>>>>> = ([{}][{}][{<([{}])>}])
The use of native transforming of input this way (auto syntax of language) is not allowed.
As always: shortest code wins.