The task
Most Casio calculators featured a variable M
, which has 3 operators related to it: M
, M+
, and M-
M
, as you can expect, returns the value stored in the variable.
M+
takes the number on the left and add it to M
.
M-
does the same thing as M+
, but subtract instead.
To make the challenge just a bit more difficult, we will add an extra operator: MR
, which is short for Memory reset
. As you can guess from the name, it resets the variable M
back to 0.
Your task is to parse a string of Casio M
expression, then print the value of M
.
Here's an example:
9M+9M+
Here, we add 9
to variable M
twice, so the output will be 18
.
Specifications
M
will be0
when first initialized- For this challenge, you don't need to handle basic arithmetics, just those operators.
- However, you do need to handle implicit multiplications, so
9M
will become81
forM = 9
- If the expression given is invalid, you can return any value of your choice
M+
andM-
must not have an operator behind them (except forM
), so cases likeMRM+
and9M+M+
are invalid, but9M+MM+
is valid.
Input/Output
The input should be a string and the output is either the variable M
or an invalid value of your choice.
Testcases
Input -> Output
9M+9M+ -> 18
9MR9M+ -> 9
9M+MM+ -> 18
9M-9M-9M+ -> -9
9M+MMM+ -> 90
81M+81M+56M-35M-12M+ -> 83
9M+MRM+ -> Invalid
9M+M+ -> Invalid
9 -> 0 (Explanation: does not modify M with an operator)
9M+MM -> 9 (Same as the above test case)
2M+2M2MM+ -> 18
9M+MMM+ -> 90
? \$\endgroup\$2M+2M2MM+
be valid? If so, I assume the answer would be18
? \$\endgroup\$18
\$\endgroup\$2M+2M2MM+
to the test case, because only one of the current four answers handles it correctly.. :/ \$\endgroup\$