yahoo

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 except ValueError:
57 # Not a number, check if it's a known operation
58 if token in operations:
59 # Special handling for "multiplied by" and "divided by"
60 if token == 'multiplied':
61 # Edge Case: "multiplied" not followed by "by"
62 if i + 1 >= len(tokens) or tokens[i + 1] != 'by':
63 raise ValueError("syntax error")
64 parsed_tokens.append('*')
65 i += 1 # Skip "by"
66 elif token == 'divided':
67 # Edge Case: "divided" not followed by "by"
68 if i + 1 >= len(tokens) or tokens[i + 1] != 'by':
69 raise ValueError("syntax error")
70 parsed_tokens.append('/')
71 i += 1 # Skip "by"
72 else:
73 parsed_tokens.append(operations[token])
74 else:
75 # Edge Case: Unknown operation
76 raise ValueError("unknown operation")
77
78 i += 1
79
80 # Edge Case: Expression with just a number
81 if len(parsed_tokens) == 1:
82 if isinstance(parsed_tokens[0], int):
83 return parsed_tokens[0]
84 else:
85 # This shouldn't happen with our parsing logic, but just in case
86 raise ValueError("syntax error")
87
88 # Edge Case: Even number of tokens (should be odd: number, operation, number, ...)
89 if len(parsed_tokens) % 2 == 0:
90 raise ValueError("syntax error")
91
92 # Evaluate from left to right
93 result = parsed_tokens[0]
94
95 # Edge Case: First token is not a number
96 if not isinstance(result, int):
97 raise ValueError("syntax error")
98
99 # Process operations
100 for i in range(1, len(parsed_tokens), 2):
101 # Edge Case: Missing operation or number
102 if i + 1 >= len(parsed_tokens):
103 raise ValueError("syntax error")
104
105 operation = parsed_tokens[i]
106 next_number = parsed_tokens[i + 1]
107
108 # Edge Case: Operation is not a string or next_number is not an int
109 if not isinstance(operation, str) or not isinstance(next_number, int):
110 raise ValueError("syntax error")
111
112 # Edge Case: Consecutive operations
113 if isinstance(parsed_tokens[i-1], str) and isinstance(operation, str):
114 raise ValueError("syntax error")
115
116 if operation == '+':
117 result += next_number
118 elif operation == '-':
119 result -= next_number
120 elif operation == '*':
121 result *= next_number
122 elif operation == '/':
123 # Edge Case: Division by zero
124 if next_number == 0:
125 raise ValueError("syntax error")
126 result //= next_number # Integer division as per examples
127 else:
128 # Edge Case: Unsupported operation
129 raise ValueError("unknown operation")
130
131 return result
132 # Handled Edge Cases: Empty input, None input, questions not starting with "What is",
133 # empty expression, no tokens, unknown operations, "multiplied/divided"
134 # not followed by "by", expression with just a number, even number of tokens,
135 # first token not a number, missing operation or number, operation is not a string,
136 # next_number is not an int, consecutive operations, division by zero,
137 # unsupported operation
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.