Created
October 4, 2023 17:52
-
-
Save braddotcoffee/149dd8a895578a415c347876fecfe1ad to your computer and use it in GitHub Desktop.
150. Evaluate Reverse Polish Notation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
VALID_OPERATIONS = set(["+", "-", "*", "/"]) | |
def evalRPN(self, tokens: List[str]) -> int: | |
nums = [] | |
for token in tokens: | |
if token not in Solution.VALID_OPERATIONS: | |
nums.append(int(token)) | |
continue | |
operand_two = nums.pop() | |
operand_one = nums.pop() | |
if token == "+": | |
nums.append(operand_one + operand_two) | |
if token == "-": | |
nums.append(operand_one - operand_two) | |
if token == "*": | |
nums.append(operand_one * operand_two) | |
if token == "/": | |
nums.append(int(operand_one / operand_two)) | |
return nums[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment