awesome

Finished
11def answer(question: str) -> int:
2 pass
2 # Edge Case: Empty question
3 if not question:
4 raise ValueError("syntax error")
5
6 # Edge Case: Non-math questions
7 if not question.startswith("What is"):
8 raise ValueError("unknown operation")
9
10 # Remove the "What is " prefix and the trailing "?"
11 if not question.endswith('?'):
12 raise ValueError("syntax error")
13 expression = question[8:-1].strip()
14
15 # Edge Case: Empty expression after "What is"
16 if not expression:
17 raise ValueError("syntax error")
18
19 # Split the expression into tokens
20 tokens = expression.split()
21
22 # Edge Case: Invalid syntax - operation at the beginning
23 if tokens[0] in ["plus", "minus", "multiplied", "divided"]:
24 raise ValueError("syntax error")
25
26 # Try to parse the first number
27 try:
28 result = int(tokens[0])
29 except ValueError:
30 # Edge Case: First token is not a number
31 raise ValueError("syntax error")
32
33 i = 1
34 while i < len(tokens):
35 operation = tokens[i]
36
37 # Edge Case: Invalid syntax - unexpected token
38 # First check if it's a valid operation
39 if operation not in ["plus", "minus", "multiplied", "divided"]:
40 # If it's not a valid operation, check if it's a number
41 try:
42 int(operation)
43 # If it's a number, this is invalid syntax (number in operation position)
44 raise ValueError("syntax error")
45 except ValueError as e:
46 # If conversion to int fails, it's not a number, so it's an unknown operation
47 # But we need to be careful not to catch our own "syntax error" exception
48 if str(e) == "syntax error":
49 raise
50 # If it's not a number, it's an unknown operation
51 raise ValueError("unknown operation")
52
53 # Edge Case: Incomplete expression - operation without operand
54 if i + 1 >= len(tokens):
55 raise ValueError("syntax error")
56
57 # Edge Case: Invalid syntax - consecutive operations
58 if operation in ["plus", "minus", "multiplied", "divided"] and i > 0 and tokens[i-1] in ["plus", "minus", "multiplied", "divided"]:
59 raise ValueError("syntax error")
60
61 # Perform the operation
62 if operation == "plus":
63 # Get the operand
64 try:
65 operand = int(tokens[i + 1])
66 except ValueError:
67 # Edge Case: Operand is not a number
68 raise ValueError("syntax error")
69 result += operand
70 elif operation == "minus":
71 # Get the operand
72 try:
73 operand = int(tokens[i + 1])
74 except ValueError:
75 # Edge Case: Operand is not a number
76 raise ValueError("syntax error")
77 result -= operand
78 elif operation == "multiplied":
79 # Edge Case: Check for correct "multiplied by" syntax
80 if i + 1 >= len(tokens) or tokens[i + 1] != "by":
81 raise ValueError("syntax error")
82 # Check if there's an operand after "by"
83 if i + 2 >= len(tokens):
84 raise ValueError("syntax error")
85 # Get the operand
86 try:
87 operand = int(tokens[i + 2])
88 except ValueError:
89 # Edge Case: Operand is not a number
90 raise ValueError("syntax error")
91 result *= operand
92 i += 1 # Skip the "by" token
93 elif operation == "divided":
94 # Edge Case: Check for correct "divided by" syntax
95 if i + 1 >= len(tokens) or tokens[i + 1] != "by":
96 raise ValueError("syntax error")
97 # Check if there's an operand after "by"
98 if i + 2 >= len(tokens):
99 raise ValueError("syntax error")
100 # Get the operand
101 try:
102 operand = int(tokens[i + 2])
103 except ValueError:
104 # Edge Case: Operand is not a number
105 raise ValueError("syntax error")
106 result //= operand
107 i += 2 # Skip the "by" token and operand
108 else:
109 # This should never happen due to earlier validation, but just in case
110 raise ValueError("unknown operation")
111
112 i += 2
113
114 return result
115 # Handled Edge Cases: Non-math questions, Empty expression, Invalid syntax (operation at beginning, consecutive operations, unexpected tokens), Unknown operations, Incomplete expressions, Invalid operands, Incorrect "multiplied by"/"divided by" syntax
Test NameStatus
test_multiple_division
Fail
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_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.