-
Notifications
You must be signed in to change notification settings - Fork 2
/
separation.py
53 lines (50 loc) · 1.59 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def parse (expression):
result = []
number = ''
for symbol in expression:
if symbol.isdigit():
number += symbol
else:
if number != '':
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
def braces(result):
if '(' in result:
index_open = result.index('(')
index_close = result.index(')')
res = calculate(result[index_open:index_close + 1])
new_list = []
first_part = result[:index_open]
if len(first_part) != 0:
new_list = first_part + [res]
second_part = result[index_close + 1:]
if len(second_part) != 0:
new_list = [res] + second_part
res = calculate(new_list)
else:
res = calculate(result)
return res