-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordy.py
More file actions
65 lines (60 loc) · 1.97 KB
/
wordy.py
File metadata and controls
65 lines (60 loc) · 1.97 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
def answer(question):
question = question.replace("What is", "").strip().rstrip("?").strip()
if not question:
raise ValueError("syntax error")
tokens = question.split()
if len(tokens) == 1:
try:
return int(tokens[0])
except ValueError:
raise ValueError("syntax error")
equation = []
i = 0
expect_number = True
while i < len(tokens):
if expect_number:
try:
equation.append(int(tokens[i]))
i += 1
expect_number = False
except ValueError:
raise ValueError("syntax error")
else:
if (i + 1 < len(tokens) and
tokens[i] in ["multiplied", "divided"] and
tokens[i + 1] == "by"):
operation = tokens[i]
i += 2
else:
operation = tokens[i]
i += 1
if operation in ["plus", "minus", "multiplied", "divided"]:
equation.append(operation)
expect_number = True
else:
is_number = True
try:
int(operation)
except ValueError:
is_number = False
if is_number:
raise ValueError("syntax error")
else:
raise ValueError("unknown operation")
if len(equation) % 2 == 0 or not equation:
raise ValueError("syntax error")
result = equation[0]
for i in range(1, len(equation), 2):
operation = equation[i]
number = equation[i + 1]
if operation == "plus":
result += number
elif operation == "minus":
result -= number
elif operation == "multiplied":
result *= number
elif operation == "divided":
if number == 0:
raise ValueError("syntax error")
result //= number
return result