String is given in form of a variable. Let's call that variable s
. It is known that the string looks like a calculation expression using +
, -
, *
, /
. There's no parentheses.
Calculate the expression inside the string and store the result in variable r
(you can assume it's already declared). All the divisions can be performed without remainders (4-6*3/8
is invalid because 8 is not divisible by 3). Length of string is not limited. String can't contain 0
.
Challenge is not supposed to give advantage for some languages, so I decided to change some math rules:
Operators precedence is a little bit different than it is in math and programming language expressions: multiplication/division is still higher than addition/substraction. However, if there're some multiplications/divisions in a row, it is performed right-to-left. Addition/substraction is preformed left-to-right.
Sample s
and r
:
2+5-6/9*8+7
-> 2
1/3*6-6
-> 12
The shortest code (in bytes) wins.
eval
equivalents. \$\endgroup\$