Skip to content

Commit

Permalink
re-solve "150. Evaluate Reverse Polish Notation"
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Oct 20, 2024
1 parent d9faf2c commit 53a54b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .plan
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ solve all "stack" questions in top-150
complete top interview 150
solve each available question at least once

Oct 20, 2024

* re-solve "150. Evaluate Reverse Polish Notation"

Oct 19, 2024

* re-solve "155. Min Stack"
Expand Down
23 changes: 12 additions & 11 deletions src/evaluate_reverse_polish_notation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ def evalRPN(self, tokens: list[str]) -> int:
b = stack.pop()
a = stack.pop()

if token == "+":
stack.append(a + b)
elif token == "-":
stack.append(a - b)
elif token == "*":
stack.append(a * b)
elif token == "/":
stack.append(int(a / b))
else:
raise ValueError(f"unknown token: {token}")
match token:
case "+":
stack.append(a + b)
case "-":
stack.append(a - b)
case "*":
stack.append(a * b)
case "/":
stack.append(int(a / b))
case _:
raise ValueError(f"unknown token: {token}")

return stack[-1]
return stack[0]

0 comments on commit 53a54b5

Please sign in to comment.