-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmir_help.py
67 lines (63 loc) · 2.05 KB
/
mir_help.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
58
59
60
61
62
63
64
65
66
67
from sympy import Eq, Symbol, solve
# Convert all numbers into floats if list contains a division
def chkfl(co_num_list):
con_div = '/'
final_con = []
if con_div in co_num_list:
for i in co_num_list:
try:
final_con.append(float(i))
except (NameError, ValueError, SyntaxError, TypeError, ZeroDivisionError):
final_con.append(i)
final_con_str = [str(thing) for thing in final_con]
exp_result = "".join(final_con_str)
else:
exp_result = "".join(co_num_list)
return exp_result
# Convert labels into math operators
def convop(od_list_co):
for n,i in enumerate(od_list_co):
if i=='div':
od_list_co[n]="/"
elif i=='x':
od_list_co[n]='*'
elif i=='^':
od_list_co[n]='**'
elif i=='sqrt':
od_list_co[n]='sqrt('
elif i=='=':
od_list_co[n]='=='
return od_list_co
# Combine intergers between operators
def combint(od_list_co):
co_num = ''
co_num_list = []
for n, i in enumerate(od_list_co):
try:
float(i)
co_num += i
if n == len(od_list_co)-1:
co_num_list.append(co_num)
except (NameError, ValueError, SyntaxError, TypeError, ZeroDivisionError):
if co_num == '':
co_num_list.append(i)
else:
co_num_list.append(co_num)
co_num = ''
co_num_list.append(i)
return co_num_list
def getresult(co_num_list, exp_result):
exp_split = exp_result.split("==")
n = Symbol('n')
if 'n' in co_num_list and '==' in co_num_list:
try:
eqn = Eq(eval(exp_split[0]), eval(exp_split[1]))
result = solve(eqn)
except (NameError, ValueError, SyntaxError, TypeError, ZeroDivisionError):
result = '...'
else:
try:
result = eval(exp_result)
except (NameError, ValueError, SyntaxError, TypeError, ZeroDivisionError):
result = '...'
return result