great

Finished
11def answer(question: str) -> int:
2 pass
2 """
3 Parse and evaluate a simple math word problem.
4
5 Args:
6 question: A string representing a math word problem
7
8 Returns:
9 int: The result of evaluating the math expression
10
11 Raises:
12 ValueError: If the question is malformed, contains unsupported operations,
13 or is not a math question
14 """
15 # Edge Case: Empty or None input
16 if not question:
17 raise ValueError("syntax error")
18
19 # Edge Case: Non-math questions
20 if not question.startswith("What is"):
21 raise ValueError("unknown operation")
22
23 # Edge Case: Malformed questions without a question mark
24 if not question.endswith("?"):
25 raise ValueError("syntax error")
26
27 # Remove "What is" prefix and question mark
28 expression = question[8:-1].strip()
29
30 # Edge Case: Empty expression after removing prefix and suffix
31 if not expression:
32 raise ValueError("syntax error")
33
34 # Tokenize the expression
35 tokens = []
36 i = 0
37 while i < len(expression):
38 if expression[i].isspace():
39 i += 1
40 continue
41
42 # Handle negative numbers
43 if expression[i] == '-' and (i == 0 or expression[i-1] in ' '):
44 i += 1
45 if i >= len(expression) or not expression[i].isdigit():
46 raise ValueError("syntax error")
47 num_start = i - 1
48 while i < len(expression) and (expression[i].isdigit() or expression[i] == '-'):
49 i += 1
50 tokens.append(int(expression[num_start:i]))
51 continue
52
53 # Handle positive numbers
54 if expression[i].isdigit():
55 num_start = i
56 while i < len(expression) and expression[i].isdigit():
57 i += 1
58 tokens.append(int(expression[num_start:i]))
59 continue
60
61 # Handle operators
62 if expression[i:].startswith("plus"):
63 tokens.append("plus")
64 i += 4
65 elif expression[i:].startswith("minus"):
66 tokens.append("minus")
67 i += 5
68 elif expression[i:].startswith("multiplied by"):
69 tokens.append("multiplied")
70 i += 13
71 elif expression[i:].startswith("divided by"):
72 tokens.append("divided")
73 i += 10
74 else:
75 # Edge Case: Unsupported operations
76 raise ValueError("unknown operation")
77
78 # Edge Case: Expression with just a number
79 if len(tokens) == 1 and isinstance(tokens[0], int):
80 return tokens[0]
81
82 # Edge Case: Invalid syntax - insufficient tokens for operations
83 if len(tokens) < 3:
84 raise ValueError("syntax error")
85
86 # Edge Case: Invalid syntax - even number of tokens (should be odd: number, operator, number, ...)
87 if len(tokens) % 2 == 0:
88 raise ValueError("syntax error")
89
90 # Evaluate the expression from left to right
91 result = tokens[0]
92
93 # Edge Case: First token is not a number
94 if not isinstance(result, int):
95 raise ValueError("syntax error")
96
97 i = 1
98 while i < len(tokens):
99 # Edge Case: Missing operator
100 if not isinstance(tokens[i], str):
101 raise ValueError("syntax error")
102
103 operator = tokens[i]
104
105 # Edge Case: Missing operand
106 if i + 1 >= len(tokens):
107 raise ValueError("syntax error")
108
109 # Edge Case: Non-numeric operand
110 if not isinstance(tokens[i + 1], int):
111 raise ValueError("syntax error")
112
113 operand = tokens[i + 1]
114
115 if operator == "plus":
116 result += operand
117 elif operator == "minus":
118 result -= operand
119 elif operator == "multiplied":
120 result *= operand
121 elif operator == "divided":
122 # Edge Case: Division by zero
123 if operand == 0:
124 raise ValueError("syntax error")
125 result //= operand # Integer division as per examples
126 else:
127 # Edge Case: Unknown operator (should not happen with current parsing)
128 raise ValueError("unknown operation")
129
130 i += 2
131
132 return result
133 # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark,
134 # empty expression, unsupported operations, just a number,
135 # insufficient tokens, even number of tokens, first token not a number,
136 # missing operators, missing operands, non-numeric operands,
137 # division by zero, unknown operators
Test NameStatus
test_addition
Pass
test_addition_and_multiplication
Pass
test_addition_and_subtraction
Pass
test_addition_with_negative_numbers
Pass
test_division
Pass
test_just_a_number
Pass
test_large_addition
Pass
test_missing_number
Pass
test_missing_operation
Pass
test_more_addition
Pass
test_multiple_additions
Pass
test_multiple_division
Pass
test_multiple_multiplication
Pass
test_multiple_subtraction
Pass
test_multiplication
Pass
test_non_math_question
Pass
test_reject_postfix_notation
Pass
test_reject_prefix_notation
Pass
test_reject_problem_missing_an_operand
Pass
test_reject_problem_with_no_operands_or_operators
Pass
test_reject_two_numbers_in_a_row
Pass
test_reject_two_operations_in_a_row
Pass
test_subtraction
Pass
test_subtraction_then_addition
Pass
test_unknown_operation
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.