From 53a54b5c6bf63a5872bc71e9d16361d2cc3d42b8 Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sun, 20 Oct 2024 16:36:39 +0200 Subject: [PATCH] re-solve "150. Evaluate Reverse Polish Notation" --- .plan | 4 ++++ src/evaluate_reverse_polish_notation.py | 23 ++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/.plan b/.plan index 40fc00f..24f573d 100644 --- a/.plan +++ b/.plan @@ -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" diff --git a/src/evaluate_reverse_polish_notation.py b/src/evaluate_reverse_polish_notation.py index c5a2512..716ddd6 100644 --- a/src/evaluate_reverse_polish_notation.py +++ b/src/evaluate_reverse_polish_notation.py @@ -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]