-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathp16_26.py
More file actions
64 lines (52 loc) · 1.45 KB
/
p16_26.py
File metadata and controls
64 lines (52 loc) · 1.45 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
from typing import List
def eval_operation(ops_string: str) -> float:
tokens = tokenise(ops_string)
reduced_tokens = []
i = 0
while i < len(tokens):
if tokens[i] == "/":
reduced_tokens[-1] /= tokens[i+1]
i += 2
elif tokens[i] == "*":
reduced_tokens[-1] *= tokens[i+1]
i += 2
else:
reduced_tokens.append(tokens[i])
i += 1
final_result = reduced_tokens[0]
i = 1
# print(tokens)
# print(reduced_tokens)
while i < len(reduced_tokens):
if reduced_tokens[i] == "+":
final_result += reduced_tokens[i+1]
elif reduced_tokens[i] == "-":
final_result -= reduced_tokens[i+1]
i += 2
return final_result
def tokenise(raw: str) -> list:
tokens = []
cnum = None # Only have positive integers
for c in raw:
if c.isdigit():
if cnum:
cnum *= 10
cnum += int(c)
else:
cnum = int(c)
elif c in "-+/*":
if cnum:
tokens.append(cnum)
cnum = None
tokens.append(c)
else:
raise Exception("Unknown charater is string: ", c)
if cnum:
tokens.append(cnum)
return tokens
if __name__ == "__main__":
exs = [
"234-134+50/25*5*2+3",
]
for ex in exs:
print(f"Result of operation {ex} is {eval_operation(ex)}")