viola

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 unknown operations, or is not a math question
13 """
14 # Edge Case: Non-math questions
15 if not question.startswith("What is"):
16 raise ValueError("unknown operation")
17
18 # Remove the "What is" prefix and the trailing question mark
19 expression = question[8:-1].strip()
20
21 # Edge Case: Empty expression after "What is"
22 if not expression:
23 raise ValueError("syntax error")
24
25 # Tokenize the expression
26 tokens = expression.split()
27
28 # Edge Case: Expression with just a number
29 if len(tokens) == 1:
30 try:
31 return int(tokens[0])
32 except ValueError:
33 # Edge Case: Single token that is not a valid integer
34 raise ValueError("syntax error")
35
36 # Parse the first number
37 try:
38 result = int(tokens[0])
39 except ValueError:
40 # Edge Case: First token is not a valid integer
41 raise ValueError("syntax error")
42
43 i = 1
44
45 # Process operations from left to right
46 while i < len(tokens):
47 # Check if we have at least an operator
48 if i >= len(tokens):
49 raise ValueError("syntax error")
50
51 operator = tokens[i]
52
53 # Edge Case: Invalid operator
54 if operator not in ["plus", "minus", "multiplied", "divided"]:
55 # Check if it's a number (syntax error) or unknown operation
56 try:
57 int(operator)
58 # If we get here, it's a number, which means syntax error (two numbers in a row)
59 raise ValueError("syntax error")
60 except ValueError as e:
61 # Check if the error is from int() conversion or from our own raise
62 if str(e) == "syntax error":
63 # This is our own raise, re-raise it
64 raise
65 else:
66 # This means it's not a valid integer, so it's an unknown operation
67 raise ValueError("unknown operation")
68
69 # Handle two-word operators
70 if operator in ["multiplied", "divided"]:
71 # Edge Case: Incomplete two-word operator
72 if i + 2 >= len(tokens):
73 raise ValueError("syntax error")
74
75 # Edge Case: Invalid second word in two-word operator
76 if tokens[i+1] != "by":
77 raise ValueError("syntax error")
78
79 i += 1 # Skip the "by" token
80
81 # Check if we have an operand after the operator
82 if i + 1 >= len(tokens):
83 raise ValueError("syntax error")
84
85 # Get the next number
86 try:
87 next_number = int(tokens[i+1])
88 except ValueError:
89 # Edge Case: Operand is not a valid integer
90 raise ValueError("syntax error")
91
92 # Perform the operation
93 if operator == "plus":
94 result += next_number
95 elif operator == "minus":
96 result -= next_number
97 elif operator == "multiplied":
98 result *= next_number
99 elif operator == "divided":
100 result //= next_number # Integer division as per examples
101
102 i += 2
103
104 return result
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.