-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_parser.py
More file actions
182 lines (154 loc) · 4.72 KB
/
math_parser.py
File metadata and controls
182 lines (154 loc) · 4.72 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from typing import Dict, Union
OPERATORS = ["^", "**", "*", "/", "+", "-"]
MAPPED_OPERATORS = [
("^", "**"),
("*", "/"),
("+", "-"),
]
NumberType = Union[int, float]
def _resolve_simple_eq(op, num1, num2) -> NumberType:
if op == "*":
return num1*num2
elif op in ["**", "^"]:
return num1**num2
elif op == "+":
return num1+num2
elif op == "-":
return num1-num2
elif op == "/":
return num1/num2
def resolve_expr(expr: str, **keys: Dict[str, str]) -> NumberType:
keys = {
" ": "",
"÷": "/",
"×": "*",
"½": "(1/2)",
"⅓": "(1/3)",
"⅔": "(2/3)",
"¼": "(1/4)",
"¾": "(3/4)",
"⅕": "(1/5)",
"⅖": "(2/5)",
"⅗": "(3/5)",
"⅘": "(4/5)",
"⅙": "(1/6)",
"⅚": "(5/6)",
"⅐": "(1/7)",
"⅛": "(1/8)",
"⅜": "(3/8)",
"⅝": "(5/8)",
"⅞": "(7/8)",
"⅑": "(1/9)",
"⅒": "(1/10)",
**keys
}
for k, v in keys.items():
expr = expr.replace(k, str(v))
listable = []
c_num = ""
for index, char in enumerate(expr):
if char == "(":
start = index
end = None
find = expr[index:]
unclosed = 0
for sub_i, sub_c in enumerate(find):
if sub_c == ")":
unclosed -= 1
elif sub_c == "(":
unclosed += 1
if unclosed == 0:
end = sub_i+index
break
new = list(expr)
new[start:end+1] = [str(resolve_expr(''.join(i for i in new[start+1:end])))]
new = ''.join(i for i in new)
return resolve_expr(new)
if char.isdigit():
c_num += char
elif char == ".":
before = None
if index-1 > -1:
before = expr[index-1]
if before is None or before in OPERATORS:
c_num += "0"+char
elif before.isdigit():
c_num += char
if char == "-":
before = None
if index-1 > -1:
before = expr[index-1]
if before is None or before in OPERATORS: # begginning of the exp OR there's already a minus sign
c_num = "-"
elif before.isdigit(): # it's a minus sign, not a negative
listable.append(c_num)
listable.append("-")
c_num = ""
if char == "+":
before = None
if index-1 > -1:
before = expr[index-1]
if before is None or before == "+":
c_num = ""
elif before.isdigit():
listable.append(c_num)
listable.append("+")
c_num = ""
if char == "*":
before = None
if index-1 > -1:
before = expr[index-1]
after = None
if index-1 > -1:
after = expr[index+1]
if before == "*":
listable.append(c_num+"*")
c_num = ""
elif after == "*":
listable.append(c_num)
c_num = "*"
else:
listable.append(c_num)
listable.append("*")
c_num = ""
if char == "/":
listable.append(c_num)
listable.append("/")
c_num = ""
if char == "^":
listable.append(c_num)
listable.append("^")
c_num = ""
if c_num:
listable.append(c_num)
return resolve_from_listable(listable)
def to_num_type(string: str) -> NumberType:
if string.endswith(".0"):
string = string.split(".", 1)[0]
if "." in string:
try:
res = float(string)
except ValueError:
return int(string)
else:
return res
return int(string)
def resolve_from_listable(listable) -> NumberType:
for ops in MAPPED_OPERATORS:
while True:
shortest_res = {}
for op in ops:
try:
ind = listable.index(op)
except ValueError:
continue
else:
shortest_res[ind] = op
if not shortest_res:
break
smallest = min(shortest_res.keys())
the_op = shortest_res[smallest]
num1, num2 = to_num_type(str(listable[smallest-1])), to_num_type(str(listable[smallest+1]))
result = to_num_type(str(_resolve_simple_eq(the_op, num1, num2)))
listable[smallest-1:smallest+2] = [result]
return listable[0]