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 unknown operations, or is not a math question
13 """
14 # Edge Case: Empty or None input
15 if not question:
16 raise ValueError("syntax error")
17
18 # Edge Case: Question doesn't end with a question mark
19 if not question.endswith("?"):
20 raise ValueError("syntax error")
21
22 # Edge Case: Question doesn't start with "What is"
23 if not question.startswith("What is"):
24 raise ValueError("syntax error")
25
26 # Remove "What is" prefix and question mark
27 expression = question[8:].rstrip('?').strip()
28
29 # Edge Case: Empty expression after removing prefix
30 if not expression:
31 raise ValueError("syntax error")
32
33 # Split the expression into tokens
34 tokens = expression.split()
35
36 # Edge Case: No tokens
37 if not tokens:
38 raise ValueError("syntax error")
39
40 # Parse the first number
41 try:
42 result = int(tokens[0])
43 position = 1
44 except ValueError:
45 # Edge Case: First token is not a number
46 raise ValueError("syntax error")
47
48 # Process operations
49 while position < len(tokens):
50 # Check for valid operations
51 op = tokens[position]
52 position += 1
53
54 # Handle two-word operations
55 if op == "multiplied" or op == "divided":
56 if position >= len(tokens):
57 raise ValueError("syntax error")
58
59 if op == "multiplied" and tokens[position] == "by":
60 op = "multiplied by"
61 position += 1
62 elif op == "divided" and tokens[position] == "by":
63 op = "divided by"
64 position += 1
65 else:
66 # Unknown operation
67 raise ValueError("unknown operation")
68 elif op not in ["plus", "minus"]:
69 # Unknown single-word operation
70 raise ValueError("unknown operation")
71
72 # Edge Case: Missing number after operation
73 if position >= len(tokens):
74 raise ValueError("syntax error")
75
76 # Parse next number
77 try:
78 num = int(tokens[position])
79 except ValueError:
80 # Edge Case: Token after operation is not a number
81 # Check if this is an unknown operation
82 if tokens[position] not in ["plus", "minus", "multiplied", "divided"]:
83 # This is not a number and not a known operation - check if it's an unknown operation
84 if position + 1 < len(tokens) and tokens[position + 1] == "by":
85 # This looks like an unknown two-word operation
86 raise ValueError("unknown operation")
87 else:
88 # This is an unknown single-word operation
89 raise ValueError("unknown operation")
90 else:
91 # This is a known operation but we're missing a number
92 raise ValueError("syntax error")
93
94 # Perform operation
95 if op == "plus":
96 result += num
97 elif op == "minus":
98 result -= num
99 elif op == "multiplied by":
100 result *= num
101 elif op == "divided by":
102 # Edge Case: Division by zero
103 if num == 0:
104 raise ValueError("syntax error")
105 result //= num
106 else:
107 # Edge Case: Unknown operation
108 raise ValueError("unknown operation")
109
110 position += 1
111
112 return result
Test NameStatus
test_missing_operation
Fail
test_non_math_question
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_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.