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" and "?" to get the expression
28 expression = question[8:-1].strip()
29
30 # Edge Case: Empty expression after removing "What is" and "?"
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) and expression[i].isdigit():
46 start = i - 1
47 while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'):
48 i += 1
49 tokens.append(int(expression[start:i]))
50 else:
51 # This is a minus operator, not a negative number
52 tokens.append('-')
53 i -= 1 # Step back to process the digit or space in next iteration
54 # Handle positive numbers
55 elif expression[i].isdigit():
56 start = i
57 while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'):
58 i += 1
59 tokens.append(int(expression[start:i]))
60 # Handle operators
61 elif expression[i:].startswith('plus'):
62 tokens.append('plus')
63 i += 4
64 elif expression[i:].startswith('minus'):
65 tokens.append('minus')
66 i += 5
67 elif expression[i:].startswith('multiplied by'):
68 tokens.append('multiplied')
69 i += 13
70 elif expression[i:].startswith('divided by'):
71 tokens.append('divided')
72 i += 10
73 else:
74 # Edge Case: Unknown operations or invalid characters
75 raise ValueError("unknown operation")
76
77 # Edge Case: Expression with just a number
78 if len(tokens) == 1 and isinstance(tokens[0], int):
79 return tokens[0]
80
81 # Edge Case: Invalid syntax - empty tokens or starts/ends with operator
82 if not tokens or not isinstance(tokens[0], int) or not isinstance(tokens[-1], int):
83 raise ValueError("syntax error")
84
85 # Evaluate the expression from left to right
86 result = tokens[0]
87 i = 1
88
89 # Edge Case: Malformed expression with incorrect operator/operand sequence
90 while i < len(tokens):
91 if i + 1 >= len(tokens):
92 raise ValueError("syntax error")
93
94 operator = tokens[i]
95 operand = tokens[i + 1]
96
97 # Edge Case: Invalid operator
98 if operator not in ['plus', 'minus', 'multiplied', 'divided']:
99 raise ValueError("syntax error")
100
101 # Edge Case: Operand is not a number
102 if not isinstance(operand, int):
103 raise ValueError("syntax error")
104
105 if operator == 'plus':
106 result += operand
107 elif operator == 'minus':
108 result -= operand
109 elif operator == 'multiplied':
110 result *= operand
111 elif operator == 'divided':
112 # Edge Case: Division by zero
113 if operand == 0:
114 raise ValueError("syntax error")
115 result //= operand # Integer division as per examples
116
117 i += 2
118
119 return result
120 # Handled Edge Cases: Empty input, None input, non-math questions, questions without question mark,
121 # empty expression, unknown operations, invalid characters, expression with just a number,
122 # invalid syntax (empty tokens, starts/ends with operator), incorrect operator/operand sequence,
123 # invalid operator, operand is not a number, division by zero
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.