-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathevaluate-reverse-polish-notation.py
69 lines (50 loc) · 1.72 KB
/
evaluate-reverse-polish-notation.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import unittest
class Solution:
def add(self, first, second):
return first + second
def substract(self, first, second):
return first - second
def multiply(self, first, second):
return first * second
def divide(self, first, second):
return first / second
def evalRPN(self, tokens: 'List[str]') -> 'int':
operators = {
"+": self.add,
"-": self.substract,
"*": self.multiply,
"/": self.divide,
}
stack = []
for token in tokens:
if token not in operators:
stack.append(token)
else:
second = int(stack.pop())
first = int(stack.pop())
stack.append(operators[token](first, second))
return int(stack.pop())
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_sum(self):
self.assertEqual(self.sol.evalRPN(["1", "3", "+"]), 4)
def test_multiply(self):
self.assertEqual(self.sol.evalRPN(["2", "3", "*"]), 6)
def test_substract(self):
self.assertEqual(self.sol.evalRPN(["2", "3", "-"]), -1)
def test_divide(self):
self.assertEqual(self.sol.evalRPN(["6", "3", "/"]), 2)
def test_complex1(self):
self.assertEqual(self.sol.evalRPN(["2", "1", "+", "3", "*"]), 9)
def test_complex2(self):
self.assertEqual(self.sol.evalRPN(["4", "13", "5", "/", "+"]), 6)
def test_complex3(self):
self.assertEqual(
self.sol.evalRPN(
["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
),
22
)
if __name__ == "__main__":
unittest.main()