hope

Finished
11def answer(question: str) -> int:
2 pass
2 # Edge Case: Empty string or None input
3 if not question:
4 raise ValueError("syntax error")
5
6 # Edge Case: Question doesn't start with "What is"
7 if not question.startswith("What is"):
8 raise ValueError("unknown operation")
9
10 # Remove "What is" prefix and question mark
11 question = question[8:].rstrip('?')
12
13 # Edge Case: Question is just "What is" with no content
14 if not question:
15 raise ValueError("syntax error")
16
17 # Tokenize the question
18 tokens = []
19 i = 0
20 while i < len(question):
21 # Skip whitespace
22 if question[i].isspace():
23 i += 1
24 continue
25
26 # Parse numbers (including negative)
27 if question[i].isdigit() or (question[i] == '-' and i < len(question) - 1 and question[i+1].isdigit()):
28 start = i
29 if question[i] == '-':
30 i += 1
31 while i < len(question) and (question[i].isdigit() or question[i] == '-'):
32 if question[i] == '-':
33 # Only allow '-' at the beginning of a number
34 if i != start:
35 raise ValueError("syntax error")
36 i += 1
37 try:
38 num = int(question[start:i])
39 tokens.append(num)
40 except ValueError:
41 raise ValueError("syntax error")
42 continue
43
44 # Parse operations
45 if question[i:].startswith("plus"):
46 tokens.append("+")
47 i += 4
48 elif question[i:].startswith("minus"):
49 tokens.append("-")
50 i += 5
51 elif question[i:].startswith("multiplied by"):
52 tokens.append("*")
53 i += 13
54 elif question[i:].startswith("divided by"):
55 tokens.append("/")
56 i += 10
57 else:
58 # Edge Case: Unknown operation
59 raise ValueError("unknown operation")
60
61 # Edge Case: No tokens
62 if not tokens:
63 raise ValueError("syntax error")
64
65 # Edge Case: Only one token but it's not a number
66 if len(tokens) == 1 and not isinstance(tokens[0], int):
67 raise ValueError("syntax error")
68
69 # Edge Case: Even number of tokens (must be odd: number, operation, number, ...)
70 if len(tokens) % 2 == 0:
71 raise ValueError("syntax error")
72
73 # Edge Case: First token is not a number
74 if not isinstance(tokens[0], int):
75 raise ValueError("syntax error")
76
77 # Evaluate from left to right
78 result = tokens[0]
79 i = 1
80 while i < len(tokens):
81 # Edge Case: Missing operation
82 if i >= len(tokens) - 1:
83 raise ValueError("syntax error")
84
85 # Edge Case: Expected operation but got number
86 if not isinstance(tokens[i], str):
87 raise ValueError("syntax error")
88
89 operation = tokens[i]
90
91 # Edge Case: Expected number but got operation
92 if not isinstance(tokens[i+1], int):
93 raise ValueError("syntax error")
94
95 operand = tokens[i+1]
96
97 if operation == "+":
98 result += operand
99 elif operation == "-":
100 result -= operand
101 elif operation == "*":
102 result *= operand
103 elif operation == "/":
104 # Edge Case: Division by zero
105 if operand == 0:
106 raise ValueError("syntax error") # Following test expectations
107 result //= operand # Integer division as per examples
108 else:
109 # Edge Case: Unsupported operation
110 raise ValueError("unknown operation")
111
112 i += 2
113
114 return result
115 # Handled Edge Cases: Empty input, non-math questions, missing content after "What is",
116 # invalid numbers, unknown operations, syntax errors (missing operands/operators),
117 # division by zero, incorrect token sequences
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.