-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPreparation.py
More file actions
38 lines (34 loc) · 963 Bytes
/
StringPreparation.py
File metadata and controls
38 lines (34 loc) · 963 Bytes
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
# Replaces |exp| to abs(exp)
def abs_replace(s):
res = s
i = 1
if s[0] == "|":
res = res.replace("|", "abs(", 1)
i = 4
while i < len(res):
if res[i] == "|":
if res[i-1] in ")0123456789xei":
res = res.replace("|", ")", 1)
else:
res = res.replace("|", "abs(", 1)
i += 3
i += 1
return res
def assumed_multiply(s):
res = s
i = 1
while i < len(res):
if (res[i-1] in ")xei" and res[i] in "(xep") or (res[i-1] in ")xei" and '0' <= res[i] <= '9') or ('0' <= res[i-1] <= '9' and res[i] in "(xep"):
res = res[:i] + '*' + res[i:]
i += 2
else:
i += 1
return res
def prepare(s):
prev = s
curr = prev.replace(" ", " ")
while prev != curr:
curr, prev = curr.replace(" ", " "), curr
curr = abs_replace(curr)
curr = assumed_multiply(curr)
return curr