Advertisement
viligen

expression_evaluator

Jan 19th, 2022
646
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from collections import deque
  2.  
  3. expression = input().split()
  4. result = deque()
  5.  
  6. for char in expression:
  7.     if not char.lstrip('-').isdigit():
  8.         while len(result) > 1:
  9.             first_number = result.popleft()
  10.             second_number = result.popleft()
  11.             if char == "+":
  12.                 result.appendleft(first_number + second_number)
  13.             elif char == "*":
  14.                 result.appendleft(first_number * second_number)
  15.             elif char == "-":
  16.                 result.appendleft(first_number - second_number)
  17.             elif char == "/":
  18.                 result.appendleft(first_number // second_number)
  19.  
  20.     else:
  21.         result.append(int(char))
  22. print(*result)
  23.  
  24.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement