-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
53 lines (36 loc) · 1.02 KB
/
solver.py
File metadata and controls
53 lines (36 loc) · 1.02 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
#Made by @LuckyMls
import re
def solver(op=None):
op = op.replace(',','.')
op = op.replace('^','**')
op = op.replace('{', '(')
op = op.replace('}', ')')
op = op.replace('[', '(')
op = op.replace(']', ')')
pattern = r'(\d+\(|\)\(|\)\d+)'
i = 0
for match in re.finditer(pattern, op):
end = match.end()-1+i
op = op[:end]+'*'+op[end:]
i+=1
i = 1
#Integrity check
try:
eval(op)
except:
print('Error. Check the expression')
else:
while op.count('(') > 0:
pre = op.split(')')[0]
calc = pre.split('(')[-1]
res = str(eval(calc))
op = op.replace('('+calc+')', res)
print(f'Passage {i}: ',op)
i+=1
print('Result: ',eval(op))
while True:
inp = input('Enter the expression:\n')
if inp.count('(') != inp.count(')'):
print('Parenthesis error, check the expression.\n')
else:
solver(inp)