Most Casio calculators featured a variable
M, which has 3 operators related to it:
M, as you can expect, returns the value stored in the variable.
M+ takes the number on the left and add it to
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
Here's an example:
Here, we add
9 to variable
M twice, so the output will be
0when 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
M = 9
- If the expression given is invalid, you can return any value of your choice
M-must not have an operator behind them (except for
M), so cases like
9M+M+are invalid, but
The input should be a string and the output is either the variable
M or an invalid value of your choice.
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 be
2M+2M2MM+to the test case, because only one of the current four answers handles it correctly.. :/ \$\endgroup\$