Gamified challenge · Stacks
The robot's program tokens is written in postfix order: each operator comes after the two values it works on. A token is either an integer (it may be negative, like -11) or one of +, -, *, /. An operator combines the two most recent results, the older one on the left: 5 2 - means 5 - 2.
Write evalRpn(tokens) (Python: eval_rpn(tokens)) that returns the final value. Division rounds toward zero: 7 -2 / is -3, not -4. Every program is valid, never divides by zero, and every value fits in a 32-bit int.
For ["3", "4", "+", "2", "*"] the robot computes 3 + 4 = 7, then 7 * 2, so the answer is 14.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google