Evaluate Reverse Polish Notation

LeetCode

LeetCode: https://leetcode.com/problems/evaluate-reverse-polish-notation/

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

1def evalRPN(tokens: List[str]) -> int:
2 stack = []
3
4 for t in tokens:
5 if t not in {"+", "-", "*", "/"}:
6 stack.append(int(t))
7 continue
8 b = stack.pop()
9 a = stack.pop()
10 if t == "+":
11 stack.append(a + b)
12 elif t == "-":
13 stack.append(a - b)
14 elif t == "*":
15 stack.append(a * b)
16 else:
17 stack.append(int(a / b))
18
19 return stack[-1]
top
2
0
tokens=["2", "1", "+", "3", "*"]
t=2
Step 1 / 6
Step 1:
Token "2" is a number. Push it onto the stack.
Pointers: top=0
Focus: select @ [0]
t="2"