Skip to content

Commit 01a55cc

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 150 Evaluate Reverse Polish Notation
1 parent 9f278e1 commit 01a55cc

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
4040
- [122 Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
4141
- [125 Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/)
4242
- [128 Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/description/)
43+
- [150 Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/description/)
4344
- [155 Min Stack](https://leetcode.com/problems/min-stack/description/)
4445
- [169 Majority Element](https://leetcode.com/problems/majority-element/description/)
4546
- [189 Rotate Array](https://leetcode.com/problems/rotate-array/description/)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def evalRPN(self, tokens: List[str]) -> int:
6+
"""
7+
You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
8+
9+
Evaluate the expression. Return an integer that represents the value of the expression.
10+
11+
Note that:
12+
- The valid operators are '+', '-', '*', and '/'.
13+
- Each operand may be an integer or another expression.
14+
- The division between two integers always truncates toward zero.
15+
- There will not be any division by zero.
16+
- The input represents a valid arithmetic expression in a reverse polish notation.
17+
- The answer and all the intermediate calculations can be represented in a 32-bit integer.
18+
"""
19+
stack = []
20+
for t in tokens:
21+
if t == "+":
22+
second = stack.pop()
23+
first = stack.pop()
24+
stack.append(int(first + second))
25+
elif t == "*":
26+
second = stack.pop()
27+
first = stack.pop()
28+
stack.append(int(first * second))
29+
elif t == "-":
30+
second = stack.pop()
31+
first = stack.pop()
32+
stack.append(int(first - second))
33+
elif t == "/":
34+
second = stack.pop()
35+
first = stack.pop()
36+
stack.append(int(first / second))
37+
else:
38+
stack.append(int(t))
39+
return stack[0]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._150_evaluate_reverse_polish_notation import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["tokens", "expected"],
10+
argvalues=[
11+
(["2", "1", "+", "3", "*"], 9),
12+
(["4", "13", "5", "/", "+"], 6),
13+
(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"], 22),
14+
],
15+
)
16+
def test_func(tokens: List[str], expected: int):
17+
value = Solution().evalRPN(tokens)
18+
assert value == expected

0 commit comments

Comments
 (0)