-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathOp.py
199 lines (154 loc) · 6.21 KB
/
mathOp.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import re
from termcolor import colored as coloured
# compile our regular expression patterns
pattern = re.compile("(?<=[0-9])(?=([-+*/\^]|$))")
sectionX = re.compile("[0-9]+(\*{2}|\^)[0-9]+")
section = re.compile("[0-9]+(\*|\/)[0-9]+")
xmatch = re.compile("\*{1,2}|\^|\/")
def floatify(x, default=0):
try: return float(x)
except: return default
# count how many order / division / multiplication terms are present in an equation
def countX(x):
return len(xmatch.findall(x))
# embrace ODM terms in a braces / parentheses block
def embraceX(x):
x_ = x
# orders are prioritised
for a in sectionX.finditer(x):
if "**" not in x_:
break
t = x[a.start() : a.end()]
x_ = x_.replace(t, f"({t})")
# division and multiplication operations follow after
for a in sectionX.finditer(x):
t = x[a.start() : a.end()]
x_ = x_.replace(t, f"({t})")
return x_
def solveEquation(eqstr, verbose=False):
terms = []
total = 0
# last start index
lastIndex = 0
# remove whitespace from the equation string
eqstr = eqstr.replace(" ", "")
# embrace ODM terms
if countX(eqstr) > 1:
eqstr = embraceX(eqstr)
# check if parentheses count checks out
if eqstr.count("(") - eqstr.count(")") != 0:
print("A parentheses block isn't closed.")
return total
# parentheses block variables
depth = 0
blockStart = 0
finalEqstr = eqstr
# solve parentheses blocks
for i in range(len(eqstr)):
# set the start of block
if eqstr[i] == '(':
if depth == 0:
blockStart = i
# increase our block depth
depth += 1
# terminate parentheses check when block depth is zero
if eqstr[i] == ')':
depth -= 1
# block zero depth
if depth == 0:
parenthesesBlock = eqstr[blockStart + 1 : i]
# solve the extracted equation
result = str(solveEquation(parenthesesBlock, verbose))
# and replace the parentheses block with our result
finalEqstr = finalEqstr.replace(f"({parenthesesBlock})", f"b{result}")
# split equation string into terms
for match in pattern.finditer(finalEqstr):
term = finalEqstr[lastIndex : match.start()]
lastIndex = match.start()
# insert it to our terms list
terms.append(term)
# our queue of arithmetical operations
operationsQueue = []
# reference to our last term
lastTerm = 0
# re-order operations in a queue according to the BODMAS / PEMDAS order of operations
for term in terms:
if verbose: print(f"Term: {term}, Last Term: {lastTerm}")
if term.startswith('**') or term.startswith('^') or term.startswith('*') or term.startswith('/'):
if lastTerm in operationsQueue:
operationsQueue.remove(lastTerm)
# insert it with our ODM terms
if type(lastTerm) is not list:
lastTerm = [lastTerm, term]
else:
lastTerm = lastTerm + [term]
operationsQueue.insert(0, lastTerm)
else:
# add operation to queue
operationsQueue.append(term)
# last term reference
lastTerm = term
# turn our items into tuples
operationsQueue = [tuple(x) if type(x) is list else tuple([x]) for x in operationsQueue]
if verbose: print(f"\n{operationsQueue}")
# solve it, at long last
for queueItem in operationsQueue:
# current item total
itemTotal = 0
# evaluate items in our queue
for item in queueItem:
# strip brackets marker
if 'b' in item:
item = item.replace('b', '')
# determine which operation we should use
# ORDER operations
# power / repeated multiplication
if item.startswith('^'):
itemTotal **= floatify(item[1::])
elif item.startswith("**"):
itemTotal **= floatify(item[2::])
# DM operations
# multiplication operation
elif item.startswith('*'):
itemTotal *= floatify(item[1::])
# division operation
elif item.startswith('/'):
try:
itemTotal /= floatify(item[1::])
except ZeroDivisionError:
print(f"Trying to divide by zero! Excluding {item} from this equation.")
# AS operations
# subtraction operation
elif item.startswith('-'):
itemTotal -= floatify(item[1::])
# addition operation
elif item.startswith('+'):
itemTotal += floatify(item[1::])
else:
# if a term has no operation symbol, use addition, by default
itemTotal += floatify(item)
# combine the totals
total += itemTotal
# return the result of our equation
return total
def solve(x):
print(f"\nSolving for \"{x}\"...")
print(coloured(f"{solveEquation(x, True):,.2f}", "blue"))
# solve a bunch of equations
solve("(10 * 50) * ((20 + 5) * 100)") # expected: 1,250,000
solve("((((50 + 20) - 30) * 10) + ((30 / 2) * 12)) / 10") # expected: 58
solve("10 - 30 * ((50) + (20)) - 10") # expected: -2,100
solve("(2 ** 8) ** 2") # expected: 65,536
solve("((10 + 5) * 12) - (12 + 5)") # expected: 163
solve("7 - 1 * 0 + 3 / 3") # expected: 8
solve("1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 * 0 + 1") # expected: 12
solve("2 + (10 * 20 + 30 / 10 * 3) + (50)") # expected: 261
solve("10 * 20 + 30 / 10 * 3") # expected: 209
solve("2 + (10 * 20 + 30 / 10 * 3) + (50) * 12 / 12 * 32 * 32 / 20") # expected 2,771
solve("2 * 2 * 2 * 2 * 2 * 2 + 12 ** 3 * 2 / 100 * 3") # expected 167.68
solve("5 / 10 * 30 / 10 + (2 ** 8 - 20 / 2 * 30)") # expected -42.5
solve("1200 + 2800 * 30 + (50 + (20 * (30) - 12) * 2) + 8 ** 4 / 2 + 1 - 100") # expected 88,375
solve("2 * 3 ** 4 * 5 ** 6 * 2 * 7 / 100 / 20 / 50") # expected 354.375
solve("(20 * 30) * (20 * (2 ** 3 / 1000)) * (((30 * 20) + 10 - 30) - 10 * 20) / 50 / 10") # expected 72.96
solve("4^3 * 12 / 2") # expected 384
solve("2^2^2 + 30/2^3") # expected 19.75