Skip to content

Commit 3ee1da9

Browse files
committed
feat: add more question
1 parent 1107cdf commit 3ee1da9

File tree

8 files changed

+246
-7
lines changed

8 files changed

+246
-7
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"problem_name": "evaluate_reverse_polish_notation",
3+
"solution_class_name": "Solution",
4+
"problem_number": "150",
5+
"problem_title": "Evaluate Reverse Polish Notation",
6+
"difficulty": "Medium",
7+
"topics": "Array, Math, Stack",
8+
"tags": ["grind-75"],
9+
"readme_description": "You are given an array of strings `tokens` that represents an arithmetic expression in a **Reverse Polish Notation**.\n\nEvaluate the expression. Return *an integer that represents the value of the expression*.\n\n**Note that:**\n\n- The valid operators are `'+'`, `'-'`, `'*'`, and `'/'`.\n- Each operand may be an integer or another expression.\n- The division between two integers always **truncates toward zero**.\n- There will not be any division by zero.\n- The input represents a valid arithmetic expression in a reverse polish notation.\n- The answer and all the intermediate calculations can be represented in a **32-bit** integer.",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: tokens = [\"2\",\"1\",\"+\",\"3\",\"*\"]\nOutput: 9\n```\n**Explanation:** ((2 + 1) * 3) = 9"
13+
},
14+
{
15+
"content": "```\nInput: tokens = [\"4\",\"13\",\"5\",\"/\",\"+\"]\nOutput: 6\n```\n**Explanation:** (4 + (13 / 5)) = 6"
16+
},
17+
{
18+
"content": "```\nInput: tokens = [\"10\",\"6\",\"9\",\"3\",\"+\",\"-11\",\"*\",\"/\",\"*\",\"17\",\"+\",\"5\",\"+\"]\nOutput: 22\n```\n**Explanation:** ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 = 22"
19+
}
20+
],
21+
"readme_constraints": "- `1 <= tokens.length <= 10^4`\n- `tokens[i]` is either an operator: `\"+\"`, `\"-\"`, `\"*\"`, or `\"/\"`, or an integer in the range `[-200, 200]`.",
22+
"readme_additional": "",
23+
"solution_imports": "",
24+
"solution_methods": [
25+
{
26+
"name": "eval_rpn",
27+
"parameters": "tokens: list[str]",
28+
"return_type": "int",
29+
"dummy_return": "0"
30+
}
31+
],
32+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
33+
"test_class_name": "EvaluateReversePolishNotation",
34+
"test_helper_methods": [
35+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
36+
],
37+
"test_methods": [
38+
{
39+
"name": "test_eval_rpn",
40+
"parametrize": "tokens, expected",
41+
"parametrize_typed": "tokens: list[str], expected: int",
42+
"test_cases": "[([\"2\", \"1\", \"+\", \"3\", \"*\"], 9), ([\"4\", \"13\", \"5\", \"/\", \"+\"], 6), ([\"10\", \"6\", \"9\", \"3\", \"+\", \"-11\", \"*\", \"/\", \"*\", \"17\", \"+\", \"5\", \"+\"], 22)]",
43+
"body": "result = self.solution.eval_rpn(tokens)\nassert result == expected"
44+
}
45+
],
46+
"playground_imports": "from solution import Solution",
47+
"playground_test_case": "# Example test case\ntokens = [\\\"2\\\", \\\"1\\\", \\\"+\\\", \\\"3\\\", \\\"*\\\"]\nexpected = 9",
48+
"playground_execution": "result = Solution().eval_rpn(tokens)\nresult",
49+
"playground_assertion": "assert result == expected"
50+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= binary_tree_right_side_view
2+
PROBLEM ?= evaluate_reverse_polish_notation
33
FORCE ?= 0
44

55
sync_submodules:

leetcode/binary_tree_right_side_view/playground.ipynb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 1,
66
"id": "imports",
77
"metadata": {},
88
"outputs": [],
@@ -14,7 +14,7 @@
1414
},
1515
{
1616
"cell_type": "code",
17-
"execution_count": null,
17+
"execution_count": 2,
1818
"id": "setup",
1919
"metadata": {},
2020
"outputs": [],
@@ -26,10 +26,21 @@
2626
},
2727
{
2828
"cell_type": "code",
29-
"execution_count": null,
29+
"execution_count": 3,
3030
"id": "execute",
3131
"metadata": {},
32-
"outputs": [],
32+
"outputs": [
33+
{
34+
"data": {
35+
"text/plain": [
36+
"[1, 3, 4]"
37+
]
38+
},
39+
"execution_count": 3,
40+
"metadata": {},
41+
"output_type": "execute_result"
42+
}
43+
],
3344
"source": [
3445
"root = TreeNode.from_list(root_list)\n",
3546
"result = Solution().right_side_view(root)\n",
@@ -38,7 +49,7 @@
3849
},
3950
{
4051
"cell_type": "code",
41-
"execution_count": null,
52+
"execution_count": 4,
4253
"id": "test",
4354
"metadata": {},
4455
"outputs": [],
@@ -61,7 +72,7 @@
6172
"file_extension": ".py",
6273
"mimetype": "text/x-python",
6374
"name": "python",
64-
"nbconvert_exporter": "python3",
75+
"nbconvert_exporter": "python",
6576
"pygments_lexer": "ipython3",
6677
"version": "3.13.7"
6778
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Evaluate Reverse Polish Notation
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, Math, Stack
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 150](https://leetcode.com/problems/evaluate-reverse-polish-notation/description/)
8+
9+
## Problem Description
10+
11+
You are given an array of strings `tokens` that represents an arithmetic expression in a **Reverse Polish Notation**.
12+
13+
Evaluate the expression. Return _an integer that represents the value of the expression_.
14+
15+
**Note that:**
16+
17+
- The valid operators are `'+'`, `'-'`, `'*'`, and `'/'`.
18+
- Each operand may be an integer or another expression.
19+
- The division between two integers always **truncates toward zero**.
20+
- There will not be any division by zero.
21+
- The input represents a valid arithmetic expression in a reverse polish notation.
22+
- The answer and all the intermediate calculations can be represented in a **32-bit** integer.
23+
24+
## Examples
25+
26+
### Example 1:
27+
28+
```
29+
Input: tokens = ["2","1","+","3","*"]
30+
Output: 9
31+
```
32+
33+
**Explanation:** ((2 + 1) \* 3) = 9
34+
35+
### Example 2:
36+
37+
```
38+
Input: tokens = ["4","13","5","/","+"]
39+
Output: 6
40+
```
41+
42+
**Explanation:** (4 + (13 / 5)) = 6
43+
44+
### Example 3:
45+
46+
```
47+
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
48+
Output: 22
49+
```
50+
51+
**Explanation:** ((10 _ (6 / ((9 + 3) _ -11))) + 17) + 5 = 22
52+
53+
## Constraints
54+
55+
- `1 <= tokens.length <= 10^4`
56+
- `tokens[i]` is either an operator: `"+"`, `"-"`, `"*"`, or `"/"`, or an integer in the range `[-200, 200]`.

leetcode/evaluate_reverse_polish_notation/__init__.py

Whitespace-only changes.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "setup",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Example test case\n",
21+
"tokens = [\"2\", \"1\", \"+\", \"3\", \"*\"]\n",
22+
"expected = 9"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 3,
28+
"id": "execute",
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"data": {
33+
"text/plain": [
34+
"9"
35+
]
36+
},
37+
"execution_count": 3,
38+
"metadata": {},
39+
"output_type": "execute_result"
40+
}
41+
],
42+
"source": [
43+
"result = Solution().eval_rpn(tokens)\n",
44+
"result"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 4,
50+
"id": "test",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"assert result == expected"
55+
]
56+
}
57+
],
58+
"metadata": {
59+
"kernelspec": {
60+
"display_name": "leetcode-py-py3.13",
61+
"language": "python",
62+
"name": "python3"
63+
},
64+
"language_info": {
65+
"codemirror_mode": {
66+
"name": "ipython",
67+
"version": 3
68+
},
69+
"file_extension": ".py",
70+
"mimetype": "text/x-python",
71+
"name": "python",
72+
"nbconvert_exporter": "python",
73+
"pygments_lexer": "ipython3",
74+
"version": "3.13.7"
75+
}
76+
},
77+
"nbformat": 4,
78+
"nbformat_minor": 5
79+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
# Time: O(n)
3+
# Space: O(n)
4+
def eval_rpn(self, tokens: list[str]) -> int:
5+
stack: list[int] = []
6+
ops = {
7+
"+": lambda a, b: a + b,
8+
"-": lambda a, b: a - b,
9+
"*": lambda a, b: a * b,
10+
"/": lambda a, b: int(a / b),
11+
}
12+
13+
for token in tokens:
14+
if token in ops:
15+
b, a = stack.pop(), stack.pop()
16+
stack.append(ops[token](a, b))
17+
else:
18+
stack.append(int(token))
19+
20+
return stack[0]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
3+
from leetcode_py.test_utils import logged_test
4+
5+
from .solution import Solution
6+
7+
8+
class TestEvaluateReversePolishNotation:
9+
def setup_method(self):
10+
self.solution = Solution()
11+
12+
@pytest.mark.parametrize(
13+
"tokens, expected",
14+
[
15+
(["2", "1", "+", "3", "*"], 9),
16+
(["4", "13", "5", "/", "+"], 6),
17+
(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"], 22),
18+
],
19+
)
20+
@logged_test
21+
def test_eval_rpn(self, tokens: list[str], expected: int):
22+
result = self.solution.eval_rpn(tokens)
23+
assert result == expected

0 commit comments

Comments
 (0)