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: 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 # Edge Case: Question too short after removing prefix
12 if len(question) <= 8:
13 raise ValueError("syntax error")
14
15 expression = question[8:-1].strip()
16
17 # Edge Case: Empty expression after removing prefix
18 if not expression:
19 raise ValueError("syntax error")
20
21 # Tokenize the expression
22 tokens = []
23 i = 0
24
25 # Edge Case: Invalid syntax - leading operators
26 while i < len(expression):
27 if expression[i].isspace():
28 i += 1
29 continue
30
31 # Parse numbers (including negative)
32 if expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()):
33 start = i
34 if expression[i] == '-':
35 i += 1
36 while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'):
37 i += 1
38 # Edge Case: Invalid number format (decimals not allowed per examples)
39 num_str = expression[start:i]
40 if '.' in num_str:
41 raise ValueError("syntax error")
42 tokens.append(int(num_str))
43 # Parse operators
44 elif expression[i:].startswith('plus'):
45 tokens.append('plus')
46 i += 4
47 elif expression[i:].startswith('minus'):
48 tokens.append('minus')
49 i += 5
50 elif expression[i:].startswith('multiplied by'):
51 tokens.append('multiplied')
52 i += 13
53 elif expression[i:].startswith('divided by'):
54 tokens.append('divided')
55 i += 10
56 else:
57 # Edge Case: Unknown operations
58 raise ValueError("unknown operation")
59
60 # Edge Case: Empty tokens list
61 if not tokens:
62 raise ValueError("syntax error")
63
64 # Edge Case: Expression starts with an operator
65 if isinstance(tokens[0], str):
66 raise ValueError("syntax error")
67
68 # Evaluate the expression from left to right
69 result = tokens[0]
70 i = 1
71
72 # Edge Case: Invalid syntax - odd number of tokens (missing operator or operand)
73 if len(tokens) % 2 == 0:
74 raise ValueError("syntax error")
75
76 while i < len(tokens):
77 # Edge Case: Missing operator
78 if not isinstance(tokens[i], str):
79 raise ValueError("syntax error")
80
81 # Edge Case: Missing operand
82 if i + 1 >= len(tokens) or isinstance(tokens[i + 1], str):
83 raise ValueError("syntax error")
84
85 operator = tokens[i]
86 operand = tokens[i + 1]
87
88 if operator == 'plus':
89 result += operand
90 elif operator == 'minus':
91 result -= operand
92 elif operator == 'multiplied':
93 result *= operand
94 elif operator == 'divided':
95 # Edge Case: Division by zero
96 if operand == 0:
97 raise ValueError("syntax error")
98 result //= operand # Integer division as per examples
99 else:
100 # Edge Case: Unknown operation
101 raise ValueError("unknown operation")
102
103 i += 2
104
105 return result
106 # Handled Edge Cases: Empty question, Question doesn't start with "What is", Question too short, Empty expression, Invalid number format, Unknown operations, Empty tokens list, Expression starts with operator, Invalid syntax (odd tokens), Missing operator, Missing operand, Division by zero
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.