tobe

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: Non-integer numbers
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: 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 between numbers
78 if not isinstance(tokens[i], str):
79 raise ValueError("syntax error")
80
81 # Edge Case: Missing operand after operator
82 if i + 1 >= len(tokens):
83 raise ValueError("syntax error")
84
85 # Edge Case: Operator where operand should be
86 if isinstance(tokens[i + 1], str):
87 raise ValueError("syntax error")
88
89 operator = tokens[i]
90 operand = tokens[i + 1]
91
92 if operator == 'plus':
93 result += operand
94 elif operator == 'minus':
95 result -= operand
96 elif operator == 'multiplied':
97 result *= operand
98 elif operator == 'divided':
99 # Edge Case: Division by zero
100 if operand == 0:
101 raise ValueError("syntax error")
102 result //= operand # Integer division as per examples
103 else:
104 # Edge Case: Unknown operation during evaluation
105 raise ValueError("unknown operation")
106
107 i += 2
108
109 return result
110 # Handled Edge Cases: Empty question, Question doesn't start with "What is", Question too short after removing prefix, Empty expression after removing prefix, Non-integer numbers, Unknown operations, Empty tokens list, Expression starts with an operator, Odd number of tokens, Missing operator between numbers, Missing operand after operator, Operator where operand should be, Division by zero, Unknown operation during evaluation
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.