xas

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 An integer 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 the "What is" prefix and the question mark
28 expression = question[8:-1].strip()
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 = []
36 i = 0
37 while i < len(expression):
38 if expression[i].isspace():
39 i += 1
40 continue
41 elif expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()):
42 # Parse number (including negative numbers)
43 start = i
44 if expression[i] == '-':
45 i += 1
46 while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'):
47 i += 1
48 # Edge Case: Numbers with decimal points are not allowed
49 if '.' in expression[start:i]:
50 raise ValueError("syntax error")
51 tokens.append(int(expression[start:i]))
52 else:
53 # Parse operation words
54 if expression[i:].startswith("plus"):
55 tokens.append("plus")
56 i += 4
57 elif expression[i:].startswith("minus"):
58 tokens.append("minus")
59 i += 5
60 elif expression[i:].startswith("multiplied by"):
61 tokens.append("multiplied")
62 i += 13
63 elif expression[i:].startswith("divided by"):
64 tokens.append("divided")
65 i += 10
66 else:
67 # Edge Case: Unknown operations
68 raise ValueError("unknown operation")
69
70 # Edge Case: Expression with no tokens
71 if not tokens:
72 raise ValueError("syntax error")
73
74 # Edge Case: Expression starting with an operation
75 if isinstance(tokens[0], str):
76 raise ValueError("syntax error")
77
78 # Evaluate the expression from left to right
79 result = tokens[0]
80 i = 1
81
82 while i < len(tokens):
83 # Edge Case: Consecutive operations or numbers
84 if i + 1 >= len(tokens):
85 raise ValueError("syntax error")
86
87 if not isinstance(tokens[i], str):
88 raise ValueError("syntax error")
89
90 if isinstance(tokens[i + 1], str):
91 raise ValueError("syntax error")
92
93 operation = tokens[i]
94 operand = tokens[i + 1]
95
96 if operation == "plus":
97 result += operand
98 elif operation == "minus":
99 result -= operand
100 elif operation == "multiplied":
101 result *= operand
102 elif operation == "divided":
103 # Edge Case: Division by zero
104 if operand == 0:
105 raise ValueError("syntax error")
106 result //= operand # Integer division as per examples
107 else:
108 # Edge Case: Unsupported operations (should not happen with current parsing)
109 raise ValueError("unknown operation")
110
111 i += 2
112
113 return result
114 # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark,
115 # empty expression, numbers with decimal points, unknown operations,
116 # expression with no tokens, expression starting with operation,
117 # consecutive operations, consecutive numbers, 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.