Skip to content

Instantly share code, notes, and snippets.

@braddotcoffee
Created October 4, 2023 17:52
Show Gist options
  • Save braddotcoffee/149dd8a895578a415c347876fecfe1ad to your computer and use it in GitHub Desktop.
Save braddotcoffee/149dd8a895578a415c347876fecfe1ad to your computer and use it in GitHub Desktop.
150. Evaluate Reverse Polish Notation
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