aboutsummaryrefslogtreecommitdiff
path: root/eval_reverse_polish_notation.py
blob: f03438a60594c043ba8c3ce629921e90d158f0e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 150. Evaluate Reverse Polish Notation

"""
given an array of strings 'tokens' that represents an arithmetic expression
in a reverse polish notation. evaluate the expression. return an integer that
represents the value of the expression. note that the valid operators are + -
and /. each operand may be an integer or another expression. the division
between two integers always truncates toward zero. there will not any
division by zero. the input represents a valid arithmetic expression in
reverse polish notation. the answer and all intermediate calculations can be
represented in a 32 bit integer.
"""


class Solution(object):
    def evalRPN(self, tokens):
        """
        :type tokens: List[str]
        :rtype: int
        """
        stack = []
        for t in tokens:
            if t not in "+-*/":
                stack.append(int(t))
            else:
                r, l = stack.pop(), stack.pop()
                if t == "+":
                    stack.append(l + r)
                elif t == "-":
                    stack.append(l - r)
                elif t == "*":
                    stack.append(l * r)
                else:
                    stack.append(int(float(l) / r))
        return stack.pop()


if __name__ == "__main__":
    obj = Solution()
    print(obj.evalRPN(tokens=["2", "1", "+", "3", "*"]))
    print(obj.evalRPN(tokens=["4", "13", "5", "/", "+"]))
    print(
        obj.evalRPN(
            tokens=["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
        )
    )