-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_calc.py
53 lines (39 loc) · 1.16 KB
/
final_calc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
ID 88864381
"""
from collections import deque
from operator import add, sub, floordiv, mul
from exceptions import StackLimitException
OPERATORS = {"+": add, "-": sub, "*": mul, "/": floordiv}
class Stack:
def __init__(self):
self.__items = deque()
def push(self, item):
self.__items.append(item)
def pop(self):
try:
return self.__items.pop()
except IndexError:
raise StackLimitException("getting element from an empty stack") from None
def __repr__(self):
return f"Stack({list(self.__items)})"
def calc(args):
stack = Stack()
for arg in args:
if arg in OPERATORS:
op1, op2 = stack.pop(), stack.pop()
try:
stack.push(OPERATORS[arg](op2, op1))
except Exception as error:
raise ValueError("operation not allowed") from error
else:
try:
stack.push(int(arg))
except Exception as error:
raise ValueError("not a digit") from error
return stack.pop()
def main():
line = input().split()
print(calc(line))
if __name__ == "__main__":
main()