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 the math operation
10
11 Raises:
12 ValueError: If the question is malformed, contains unknown operations, or is not a math question
13 """
14 # Edge Case: Empty or None input
15 if not question:
16 raise ValueError("syntax error")
17
18 # Edge Case: Question doesn't start with "What is"
19 if not question.startswith("What is"):
20 raise ValueError("unknown operation")
21
22 # Edge Case: Question is just "What is" with no numbers or operations
23 if question == "What is":
24 raise ValueError("syntax error")
25
26 # Remove the "What is " prefix and the trailing question mark
27 expression = question[8:-1].strip()
28
29 # Edge Case: Empty expression after removing prefix and question mark
30 if not expression:
31 raise ValueError("syntax error")
32
33 # Split the expression into tokens
34 tokens = expression.split()
35
36 # Edge Case: No tokens
37 if not tokens:
38 raise ValueError("syntax error")
39
40 # Define operation mappings
41 operations = {
42 "plus": "+",
43 "minus": "-",
44 "multiplied": "*",
45 "divided": "/"
46 }
47
48 # Parse tokens
49 parsed_tokens = []
50 i = 0
51
52 # Edge Case: Handle malformed expressions during parsing
53 while i < len(tokens):
54 token = tokens[i]
55
56 # If token is a number (including negative numbers)
57 if token.lstrip('-').isdigit():
58 parsed_tokens.append(int(token))
59 i += 1
60 # If token is an operation
61 elif token in operations:
62 # Special handling for "multiplied by" and "divided by"
63 if token == "multiplied" or token == "divided":
64 # Edge Case: Missing "by" after "multiplied" or "divided"
65 if i + 1 >= len(tokens) or tokens[i + 1] != "by":
66 raise ValueError("syntax error")
67 parsed_tokens.append(operations[token])
68 i += 2 # Skip both the operation and "by"
69 else:
70 parsed_tokens.append(operations[token])
71 i += 1
72 else:
73 # Edge Case: Unknown operation or invalid token
74 raise ValueError("unknown operation")
75
76 # Edge Case: Expression starts with an operation
77 if len(parsed_tokens) > 0 and isinstance(parsed_tokens[0], str):
78 raise ValueError("syntax error")
79
80 # Edge Case: Expression ends with an operation
81 if len(parsed_tokens) > 0 and isinstance(parsed_tokens[-1], str):
82 raise ValueError("syntax error")
83
84 # Edge Case: Even number of tokens (should be odd: number, operation, number, ...)
85 if len(parsed_tokens) % 2 == 0:
86 raise ValueError("syntax error")
87
88 # Evaluate the expression from left to right
89 result = parsed_tokens[0]
90
91 # Edge Case: Handle division by zero
92 for i in range(1, len(parsed_tokens), 2):
93 operation = parsed_tokens[i]
94 operand = parsed_tokens[i + 1]
95
96 # Edge Case: Two consecutive numbers without operation
97 if not isinstance(operation, str):
98 raise ValueError("syntax error")
99
100 # Edge Case: Two consecutive operations
101 if not isinstance(operand, int):
102 raise ValueError("syntax error")
103
104 if operation == "+":
105 result += operand
106 elif operation == "-":
107 result -= operand
108 elif operation == "*":
109 result *= operand
110 elif operation == "/":
111 # Edge Case: Division by zero
112 if operand == 0:
113 raise ValueError("syntax error")
114 result //= operand # Integer division as per examples
115
116 return result
117 # Handled Edge Cases: Empty input, non-math questions, missing question mark,
118 # expressions starting/ending with operations,
119 # consecutive numbers/operations, division by zero,
120 # unknown operations, missing 'by' in multiplied/divided operations,
121 # even number of tokens, invalid syntax
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.