A point-of-sale device records each receipt adjustment as a flat formula: a sequence of tokens alternating value, operator, value, operator, ... , value. Each value is an integer literal (it may be negative, e.g. -7; a negative literal always has more than one character, so it is never confused with the operator token -). Each operator is one of the single characters +, -, *, /.
Evaluate the formula strictly left to right with no operator precedence at all: apply each operator to the running result and the next value, in order. For example 2 + 3 * 4 is evaluated as (2 + 3) * 4 = 20, not 2 + 12. Division truncates toward zero (like integer division in C or Java), so -7 / 2 is -3.
It is guaranteed the token count is odd (values and operators alternate, starting and ending with a value) and that division by zero never occurs.
Line 1: an integer n, the number of tokens.
Line 2: n space-separated tokens.
A single integer: the value of the formula.
Example 1
Input
5 2 + 3 * 4
Expected
20
Explanation
Left to right with no precedence: 2 + 3 = 5, then 5 * 4 = 20.
Example 2
Input
1 7
Expected
7
Explanation
A single value and no operators, so the result is 7.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →