Every phone needs a calculator app. Because of the limited screen real estate, the developer of mine has decided to save some buttons. Instead of having two dedicated bracket buttons - one for open (
and one for close )
- there is just a single bracket button ()
. It looks something like this:
When the button is pressed, it places a bracket. Based on the input given so far, it predicts whether the bracket should be open (
or close )
.
By trial and error, I have found the button to follow these rules:
- If there are no unmatched brackets, the next bracket will always be open
(
. - Else, if the last character is a numeral
0123456789
or close)
bracket, the next bracket will be close)
. - Else, if the last character is an operator
+-*/
or open(
bracket, the next bracket will be open(
.
The challenge
Based on the given input and the rules mentioned above, predict whether the button places an open (
or close )
bracket.
Input
Either a string, an array of characters, or anything equivalent. The input will only contain the following characters*: 0123456789+-*/()
Output
An open (
or close )
bracket or any two consistent values representing them.
Test cases
"" -> "("
"(1+" -> "("
"(1+(2/3" -> ")"
"(1+(2/3)" -> ")"
"(1+(2/3))-8" -> "("
"(1+(2/3))-8*(" -> "("
"(1+(2/3))-8*((5" -> ")"
"(1+(2/3))-8*((5)" -> ")"
"(1+(2/3))-8*((5))" -> "("
Scoring
This is code-golf, so the shortest answer in each language wins.
Note
* The actual calculator also includes the symbols .%
, but you don't need to care about those.
)(1+2
? \$\endgroup\$((5+2)(7+3)+4)*2
, then you have to include a multiplication sign after((5+2)
. The rules are fine in my opinion. \$\endgroup\$