Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-261

Evaluate Reverse Polish Notation

Problem

Evaluate an arithmetic expression given in reverse Polish notation, where tokens are integers or the operators +, -, * and /.

Push numbers onto a stack and, for each operator, pop the two most recent values, apply the operation and push the result. Division truncates toward zero; the input is always a valid expression.

Hint

For division, compute with absolute values and then reapply the sign to truncate toward zero.

Test cases

case 1
in: [["2","1","+","3","*"]]
expected: 9
case 2
in: [["4","13","5","/","+"]]
expected: 6
case 3
in: [["10","6","9","3","+","-11","*","/","*","17","+","5","+"]]
expected: 22
case 4
in: [["3"]]
expected: 3
case 5
in: [["7","-2","/"]]
expected: -3
Starter codePython
def eval_rpn(tokens):
    # Your code here
    pass

5 test cases · run your code against them in the editor