Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Postfix Evaluation by tokiojapan55
from typing import Union
from operator import add, sub, mul, floordiv
OPE = {"+": add, "-": sub, "*": mul, "/": floordiv}
def postfix_evaluate(items: list[Union[int, str]]) -> int:
stack = []
for item in items:
if type(item) is int:
stack.append(item)
else:
try: stack.append(OPE[item](stack.pop(-2), stack.pop()))
except: stack.append(0)
return stack.pop()
Dec. 11, 2023