Skip to content

Commit a08926e

Browse files
committed
add two more
1 parent b20f253 commit a08926e

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

Diff for: src/evaluate_reverse_polish_notation.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def evalRPN(self, tokens: List[str]) -> int:
6+
stack = []
7+
8+
for c in tokens:
9+
if c == "+":
10+
stack.append(stack.pop() + stack.pop())
11+
elif c == "-":
12+
a, b = stack.pop(), stack.pop()
13+
stack.append(b - a)
14+
elif c == "*":
15+
stack.append(stack.pop() * stack.pop())
16+
elif c == "/":
17+
a, b = stack.pop(), stack.pop()
18+
stack.append(int(b / a))
19+
else:
20+
stack.append(int(c))
21+
return stack[0]

Diff for: src/min_stack.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class MinStack:
2-
32
def __init__(self):
43
self.stack = []
54
self.minStack = []

Diff for: tests/test_evaluate_reverse_polish_notation.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from evaluate_reverse_polish_notation import Solution
2+
3+
testcase = [{"testcase": ["2", "1", "+", "3", "*"], "result": 9}]
4+
5+
6+
def test_evaluate_reverse_polish_notation():
7+
for tc in testcase:
8+
tokens = tc["testcase"]
9+
ans = Solution().evalRPN(tokens)
10+
assert ans == tc["result"]

0 commit comments

Comments
 (0)