corpus

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 "?" suffix
28 expression = question[8:-1].strip()
29
30 # Edge Case: Empty expression after removing prefix/suffix
31 if not expression:
32 raise ValueError("syntax error")
33
34 # Tokenize the expression
35 tokens = []
36 i = 0
37
38 # Edge Case: Handle negative numbers at the start
39 if expression.startswith('-'):
40 i = 1
41 while i < len(expression) and (expression[i].isdigit() or expression[i] == ' '):
42 i += 1
43 tokens.append(expression[:i])
44
45 while i < len(expression):
46 if expression[i] == ' ':
47 i += 1
48 continue
49
50 # Parse numbers
51 if expression[i].isdigit() or (expression[i] == '-' and (i == 0 or expression[i-1] == ' ')):
52 start = i
53 if expression[i] == '-':
54 i += 1
55 while i < len(expression) and expression[i].isdigit():
56 i += 1
57 tokens.append(expression[start:i])
58 # Parse operators
59 elif expression[i:].startswith('plus'):
60 tokens.append('plus')
61 i += 4
62 elif expression[i:].startswith('minus'):
63 tokens.append('minus')
64 i += 5
65 elif expression[i:].startswith('multiplied by'):
66 tokens.append('multiplied by')
67 i += 13
68 elif expression[i:].startswith('divided by'):
69 tokens.append('divided by')
70 i += 10
71 else:
72 # Edge Case: Unknown operations
73 raise ValueError("unknown operation")
74
75 # Edge Case: Single number
76 if len(tokens) == 1:
77 try:
78 return int(tokens[0])
79 except ValueError:
80 # Edge Case: Invalid number format
81 raise ValueError("syntax error")
82
83 # Edge Case: Invalid syntax - even number of tokens (should be odd: number, operator, number, ...)
84 if len(tokens) % 2 == 0:
85 raise ValueError("syntax error")
86
87 # Evaluate the expression from left to right
88 try:
89 result = int(tokens[0])
90 except ValueError:
91 # Edge Case: First token is not a valid number
92 raise ValueError("syntax error")
93
94 i = 1
95 while i < len(tokens):
96 # Edge Case: Missing operator or operand
97 if i + 1 >= len(tokens):
98 raise ValueError("syntax error")
99
100 operator = tokens[i]
101 try:
102 operand = int(tokens[i + 1])
103 except ValueError:
104 # Edge Case: Operand is not a valid number
105 raise ValueError("syntax error")
106
107 if operator == 'plus':
108 result += operand
109 elif operator == 'minus':
110 result -= operand
111 elif operator == 'multiplied by':
112 result *= operand
113 elif operator == 'divided by':
114 # Edge Case: Division by zero
115 if operand == 0:
116 raise ValueError("syntax error") # Following test expectations
117 result //= operand # Integer division as per examples
118 else:
119 # Edge Case: Unknown operator in the middle of expression
120 raise ValueError("unknown operation")
121
122 i += 2
123
124 return result
125 # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark,
126 # empty expression, negative numbers, single number, invalid syntax,
127 # unknown operations, missing operators/operands, division by zero,
128 # invalid number formats
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.