-
Notifications
You must be signed in to change notification settings - Fork 1
/
separation.py
38 lines (36 loc) · 1.11 KB
/
separation.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
from input import get_expression
def parse ():
expression = get_expression()
result = []
number = ''
# expression = expression.split()
for symbol in expression:
if symbol.isdigit():
number += symbol
else:
result.append(float(number))
number = ''
result.append(symbol)
else:
if number:
result.append(float(number))
return result
def calculate(lst):
result = 0.0
while '/' in lst:
index = lst.index('/')
result = lst[index - 1] / lst[index + 1]
lst = lst[:index -1] + [result] + lst[index + 2:]
while '*' in lst:
index = lst.index('*')
result = lst[index - 1] * lst[index + 1]
lst = lst[:index -1] + [result] + lst[index + 2:]
while '+' in lst:
index = lst.index('+')
result = lst[index - 1] + lst[index + 1]
lst = lst[:index -1] + [result] + lst[index + 2:]
while '-' in lst:
index = lst.index('-')
result = lst[index - 1] - lst[index + 1]
lst = lst[:index -1] + [result] + lst[index + 2:]
return result