This repository was archived by the owner on Mar 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwordy.py
More file actions
52 lines (45 loc) · 1.66 KB
/
Copy pathwordy.py
File metadata and controls
52 lines (45 loc) · 1.66 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
def answer(question):
words = question \
.rstrip("?") \
.replace("plus", "+") \
.replace("minus", "-") \
.replace("multiplied by", '*') \
.replace("divided by", "/") \
.replace("raised to the", "^") \
.split()
for i in range(len(words) - 2):
if words[i] == "^" and words[i + 2] == "power":
words[i + 2] = ""
if (words[i + 1][:1].isdigit()
and (words[i + 1].endswith("1st")
or words[i + 1].endswith("2nd")
or words[i + 1].endswith("th"))):
words[i + 1] = words[i + 1][:-2]
result = None
operator = None
for word in words[2:]:
if word == "":
continue
elif result is None and operator is None and isinteger(word):
result = int(word)
elif result is not None and operator is not None and isinteger(word):
if operator == "+":
result += int(word)
elif operator == "-":
result -= int(word)
elif operator == "*":
result *= int(word)
elif operator == "/":
result //= int(word)
elif operator == "^":
result **= int(word)
operator = None
elif result is not None and operator is None and word in list("+-*/^"):
operator = word
else:
raise ValueError(r".+")
if words[:2] != ["What", "is"] or result is None or operator is not None:
raise ValueError(r".+")
return result
def isinteger(number):
return number.isdigit() or (number[1:].isdigit() and number[0] == "-")