perfect

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 valid math question
14 """
15 # Edge Case: Empty or None input
16 if not question:
17 raise ValueError("syntax error")
18
19 # Edge Case: Question doesn't start with "What is"
20 if not question.startswith("What is"):
21 raise ValueError("unknown operation")
22
23 # Edge Case: Malformed question (doesn't end with '?' or is too short)
24 if not question.endswith('?'):
25 raise ValueError("syntax error")
26
27 # Extract the expression part
28 expression = question[8:-1].strip() # Remove "What is " and "?"
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 = expression.split()
36
37 # Edge Case: No tokens
38 if not tokens:
39 raise ValueError("syntax error")
40
41 # Parse the first number
42 try:
43 result = int(tokens[0])
44 tokens = tokens[1:]
45 except ValueError:
46 # Edge Case: First token is not a number
47 raise ValueError("syntax error")
48
49 # Process operations
50 i = 0
51 while i < len(tokens):
52 # Edge Case: Incomplete operation (odd number of tokens remaining)
53 if i >= len(tokens):
54 raise ValueError("syntax error")
55
56 operation = tokens[i]
57
58 # Perform the operation
59 if operation == "plus":
60 # Need one more token for the operand
61 if i + 1 >= len(tokens):
62 raise ValueError("syntax error")
63 try:
64 operand = int(tokens[i + 1])
65 except ValueError:
66 # Edge Case: Operand is not a number
67 raise ValueError("syntax error")
68 result += operand
69 i += 2
70 elif operation == "minus":
71 # Need one more token for the operand
72 if i + 1 >= len(tokens):
73 raise ValueError("syntax error")
74 try:
75 operand = int(tokens[i + 1])
76 except ValueError:
77 # Edge Case: Operand is not a number
78 raise ValueError("syntax error")
79 result -= operand
80 i += 2
81 elif operation == "multiplied":
82 # Handle "multiplied by" as a two-token operation
83 if i + 2 >= len(tokens) or tokens[i + 1] != "by":
84 raise ValueError("syntax error")
85 try:
86 operand = int(tokens[i + 2])
87 except ValueError:
88 raise ValueError("syntax error")
89 result *= operand
90 i += 3 # Skip the "multiplied", "by" tokens
91 elif operation == "divided":
92 # Handle "divided by" as a two-token operation
93 if i + 2 >= len(tokens) or tokens[i + 1] != "by":
94 raise ValueError("syntax error")
95 try:
96 operand = int(tokens[i + 2])
97 except ValueError:
98 raise ValueError("syntax error")
99 # Edge Case: Division by zero
100 if operand == 0:
101 raise ValueError("syntax error")
102 result //= operand # Integer division as per examples
103 i += 3 # Skip the "divided", "by" tokens
104 else:
105 # Edge Case: Unsupported operation
106 raise ValueError("unknown operation")
107
108 return result
109 # Handled Edge Cases: Empty input, None input, questions not starting with "What is",
110 # questions not ending with '?', empty expressions, no tokens,
111 # first token not being a number, incomplete operations,
112 # operands not being numbers, unsupported operations,
113 # division by zero
Test NameStatus
test_missing_operation
Fail
test_reject_postfix_notation
Fail
test_reject_two_numbers_in_a_row
Fail
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_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_prefix_notation
Pass
test_reject_problem_missing_an_operand
Pass
test_reject_problem_with_no_operands_or_operators
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.