relax

Finished
11def answer(question: str) -> int:
2 pass
2 """
3 Parse and evaluate a simple math word problem, returning the answer as an integer.
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 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
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: Invalid syntax - no tokens
76 if not tokens:
77 raise ValueError("syntax error")
78
79 # Edge Case: Invalid syntax - expression starts with an operator
80 if tokens[0] in ['plus', 'minus', 'multiplied by', 'divided by']:
81 raise ValueError("syntax error")
82
83 # Edge Case: Invalid syntax - expression ends with an operator
84 if tokens[-1] in ['plus', 'minus', 'multiplied by', 'divided by']:
85 raise ValueError("syntax error")
86
87 # Convert first token to number
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: Invalid syntax - consecutive operators
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 # Edge Case: Invalid syntax - consecutive operators
108 if operator not in ['plus', 'minus', 'multiplied by', 'divided by']:
109 raise ValueError("syntax error")
110
111 if operator == 'plus':
112 result += operand
113 elif operator == 'minus':
114 result -= operand
115 elif operator == 'multiplied by':
116 result *= operand
117 elif operator == 'divided by':
118 # Edge Case: Division by zero
119 if operand == 0:
120 raise ValueError("syntax error")
121 result //= operand # Integer division as per examples
122
123 i += 2
124
125 return result
126 # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark,
127 # empty expression, negative numbers, unknown operations,
128 # invalid syntax (starts/ends with operator, consecutive operators),
129 # non-numeric tokens, 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.