add postfix solns

This commit is contained in:
2022-04-13 04:55:09 +05:30
parent 658601137a
commit e91751a4ce
4 changed files with 129 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import math
from collections import deque
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
compute = {
'+': lambda a, b: a + b,
'-': lambda a, b: a - b,
'*': lambda a, b: a * b,
'/': lambda a, b: math.trunc(a / b)
}
stack = deque()
for token in tokens:
if token.lstrip('-').isdigit():
stack.append(int(token))
else:
right = stack.pop()
left = stack.pop()
stack.append(compute[token](left, right))
return stack[-1]