Skip to content

Commit 9a80565

Browse files
authored
Merge pull request #1521 from ivanpenaloza/january19
adding algos
2 parents c4db938 + 7e645a9 commit 9a80565

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def evalRPN(self, tokens: List[str]) -> int:
6+
stack = []
7+
operators = ['+', '-', '*', '/']
8+
9+
for token in tokens:
10+
if token in operators:
11+
# Pop two operands (note: order matters for - and /)
12+
b = stack.pop()
13+
a = stack.pop()
14+
15+
# Apply operation
16+
if token == '+':
17+
result = a + b
18+
elif token == '-':
19+
result = a - b
20+
elif token == '*':
21+
result = a * b
22+
else: # token == '/'
23+
# Truncate toward zero
24+
result = int(a / b)
25+
26+
stack.append(result)
27+
else:
28+
# It's a number, push to stack
29+
stack.append(int(token))
30+
31+
# Final result is the only element left in stack
32+
return stack[0]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function evalRPN(tokens: string[]): number {
2+
const stack: number[] = [];
3+
const operators: string[] = ['+', '-', '*', '/'];
4+
5+
for (const token of tokens) {
6+
if (operators.includes(token)) {
7+
// Pop two operands (note: order matters for - and /)
8+
const b = stack.pop()!;
9+
const a = stack.pop()!;
10+
11+
// Apply operation
12+
let result: number;
13+
if (token === '+') {
14+
result = a + b;
15+
} else if (token === '-') {
16+
result = a - b;
17+
} else if (token === '*') {
18+
result = a * b;
19+
} else { // token === '/'
20+
// Truncate toward zero
21+
result = Math.trunc(a / b);
22+
}
23+
24+
stack.push(result);
25+
} else {
26+
// It's a number, push to stack
27+
stack.push(parseInt(token));
28+
}
29+
}
30+
31+
// Final result is the only element left in stack
32+
return stack[0];
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_53_evaluate_reverse_polish_notation import Solution
4+
5+
class EvaluateReversePolishNotationTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.evalRPN(tokens = ["2","1","+","3","*"])
10+
target = 9
11+
self.assertEqual(output, target)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.evalRPN(tokens = ["4","13","5","/","+"])
16+
target = 6
17+
self.assertEqual(output, target)
18+
19+
def test_third_pattern(self):
20+
solution = Solution()
21+
output = solution.evalRPN(tokens = ["10","6","9","3","+","-11",
22+
"*","/","*","17","+","5","+"])
23+
target = 22
24+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)