agent

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 # Remove "What is" and the trailing question mark
24 expression = question[8:].rstrip('?')
25
26 # Edge Case: Empty expression after removing "What is" and "?"
27 if not expression:
28 raise ValueError("syntax error")
29
30 # Split the expression into tokens
31 tokens = expression.split()
32
33 # Edge Case: No tokens
34 if not tokens:
35 raise ValueError("syntax error")
36
37 # Define operation mappings
38 operations = {
39 'plus': '+',
40 'minus': '-',
41 'multiplied': '*',
42 'divided': '/'
43 }
44
45 # Parse tokens into a list of numbers and operations
46 parsed_tokens = []
47 i = 0
48
49 while i < len(tokens):
50 token = tokens[i]
51
52 # Try to parse as number
53 try:
54 num = int(token)
55 parsed_tokens.append(num)
56 i += 1
57 continue
58 except ValueError:
59 pass
60
61 # Handle operations
62 if token in operations:
63 if token == 'multiplied' or token == 'divided':
64 # Check for 'by' after 'multiplied' or 'divided'
65 # Edge Case: Missing 'by' after 'multiplied' or 'divided'
66 if i + 1 >= len(tokens) or tokens[i + 1] != 'by':
67 raise ValueError("syntax error")
68 parsed_tokens.append(operations[token])
69 i += 2 # Skip both the operation and 'by'
70 else:
71 parsed_tokens.append(operations[token])
72 i += 1
73 else:
74 # Edge Case: Unknown operation
75 raise ValueError("unknown operation")
76
77 # Edge Case: Expression with no numbers
78 if not parsed_tokens:
79 raise ValueError("syntax error")
80
81 # Edge Case: Expression starts with an operation
82 if isinstance(parsed_tokens[0], str):
83 raise ValueError("syntax error")
84
85 # Edge Case: Expression ends with an operation
86 if isinstance(parsed_tokens[-1], str):
87 raise ValueError("syntax error")
88
89 # Evaluate the expression from left to right
90 result = parsed_tokens[0]
91 i = 1
92
93 # Edge Case: Single number
94 if len(parsed_tokens) == 1:
95 return result
96
97 # Edge Case: Invalid pattern (should alternate number, operation, number, ...)
98 while i < len(parsed_tokens):
99 # Should be an operation
100 if not isinstance(parsed_tokens[i], str):
101 raise ValueError("syntax error")
102
103 # Edge Case: Not enough tokens remaining
104 if i + 1 >= len(parsed_tokens):
105 raise ValueError("syntax error")
106
107 # Should be a number
108 if not isinstance(parsed_tokens[i + 1], int):
109 raise ValueError("syntax error")
110
111 operation = parsed_tokens[i]
112 operand = parsed_tokens[i + 1]
113
114 if operation == '+':
115 result += operand
116 elif operation == '-':
117 result -= operand
118 elif operation == '*':
119 result *= operand
120 elif operation == '/':
121 # Edge Case: Division by zero
122 if operand == 0:
123 raise ValueError("syntax error")
124 result //= operand # Integer division as specified
125 else:
126 # Edge Case: Unsupported operation (should not happen with our parsing)
127 raise ValueError("unknown operation")
128
129 i += 2
130
131 return result
132 # Handled Edge Cases: Empty input, None input, questions not starting with "What is",
133 # empty expression, no tokens, missing 'by' after 'multiplied' or 'divided',
134 # unknown operations, expression with no numbers, expression starting with operation,
135 # expression ending with operation, single number, invalid pattern,
136 # not enough tokens, division by zero, unsupported operations
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.