-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_calculator.py
47 lines (37 loc) · 1.05 KB
/
final_calculator.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
# 68359374
# from operator import add, floordiv, mul, sub
# OPERATORS = {
# '+': add,
# '-': sub,
# '*': mul,
# '/': floordiv
# }
OPERATORS = {
'+': lambda x, y: x + y,
'-': lambda x, y: x - y,
'*': lambda x, y: x * y,
'/': lambda x, y: x // y
}
def read_input() -> list:
""" Read input. """
return input().strip().split()
def polish_notation(subsequence: list) -> int:
""" The function accepts a list of characters written
in reverse Polish notation. Numbers and arithmetic operations
are written with a space.
Operations can be given as input: +, -, *, / and numbers.
Input example: 1 2 + 2 +
Conclusion: 5.
"""
stack = []
for s in subsequence:
try:
if isinstance(int(s), int):
stack.append(s)
except ValueError:
num_1, num_2 = int(stack.pop()), int(stack.pop())
stack.append(OPERATORS[s](num_2, num_1))
return stack.pop()
if __name__ == '__main__':
string = read_input()
print(polish_notation(string))