-
Notifications
You must be signed in to change notification settings - Fork 0
/
polski_note.py
57 lines (45 loc) · 1.37 KB
/
polski_note.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
54
55
56
57
"""This code is release of Polski notation calculator."""
class EmptyException(Exception):
"""Exception class for empty stack."""
class Stack:
"""Stack class."""
def __init__(self):
"""Stack initial conditions."""
self.__queue = []
self.__size = 0
@property
def is_empty(self):
"""Check if stack is empty or not."""
return self.__size == 0
def push(self, item):
"""Add item to queue."""
self.__size = self.__size + 1
self.__queue.append(item)
def pop(self):
"""Remove item from queue."""
if self.is_empty:
raise EmptyException
self.size = self.__size - 1
return self.__queue.pop()
def main():
"""Input & Output main function and dictionary."""
stack = Stack()
actions = {
'*': lambda fd, ld: fd * ld,
'/': lambda fd, ld: ld // fd,
'+': lambda fd, ld: fd + ld,
'-': lambda fd, ld: ld - fd
}
item = input().split(' ')
for symbol in item:
operation = actions.get(symbol)
if operation:
fd = stack.pop()
ld = stack.pop()
result = operation(fd, ld)
stack.push(result)
else:
stack.push(int(symbol))
print(stack.pop())
if __name__ == '__main__':
main()