All problems
Postfix Expression Evaluation
Problem
Evaluate an arithmetic expression given in postfix (reverse Polish) notation.
tokens is a list of strings, each either an integer or one of the operators +, -, *, /. For each operator, pop the top two values b then a and push a op b. Division truncates toward zero.
Return the final integer result.
Hint
Operands go on the stack; an operator pops two operands and pushes the result.
Test cases
case 1
in: [["2","1","+","3","*"]]
expected: 9
case 2
in: [["4","13","5","/","+"]]
expected: 6
case 3
in: [["3","4","+","2","*"]]
expected: 14
case 4
in: [["10","6","9","3","+","-11","*","/","*","17","+","5","+"]]
expected: 22
Starter codePython
def eval_postfix(tokens):
# Your code here
pass4 test cases · run your code against them in the editor