joseph

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 # 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 and suffix
18 if not expression:
19 raise ValueError("syntax error")
20
21 # Tokenize the expression
22 tokens = []
23 i = 0
24
25 # Edge Case: Handle negative numbers
26 # If expression starts with minus, it's a negative number
27 if expression.startswith('-'):
28 i = 1
29 num_str = '-'
30 while i < len(expression) and (expression[i].isdigit() or expression[i] == ' '):
31 if expression[i] != ' ':
32 num_str += expression[i]
33 i += 1
34 if len(num_str) > 1: # Only if we actually got digits after the minus
35 tokens.append(int(num_str))
36 else:
37 raise ValueError("syntax error")
38
39 while i < len(expression):
40 if expression[i] == ' ':
41 i += 1
42 continue
43
44 # Parse numbers
45 if expression[i].isdigit() or (expression[i] == '-' and (i == 0 or expression[i-1] == ' ')):
46 num_str = ''
47 # Handle negative numbers in the middle
48 if expression[i] == '-':
49 num_str = '-'
50 i += 1
51
52 # Edge Case: Consecutive operators or malformed numbers
53 if i >= len(expression) or not expression[i].isdigit():
54 raise ValueError("syntax error")
55
56 while i < len(expression) and expression[i].isdigit():
57 num_str += expression[i]
58 i += 1
59 tokens.append(int(num_str))
60 # Parse operations
61 elif expression[i:].startswith('plus'):
62 tokens.append('plus')
63 i += 4
64 elif expression[i:].startswith('minus'):
65 tokens.append('minus')
66 i += 5
67 elif expression[i:].startswith('multiplied by'):
68 tokens.append('multiplied')
69 i += 13
70 elif expression[i:].startswith('divided by'):
71 tokens.append('divided')
72 i += 10
73 else:
74 # Edge Case: Unknown operations
75 raise ValueError("unknown operation")
76
77 # Edge Case: Expression with just a number
78 if len(tokens) == 1 and isinstance(tokens[0], int):
79 return tokens[0]
80
81 # Edge Case: Invalid syntax - insufficient tokens
82 if len(tokens) < 3:
83 raise ValueError("syntax error")
84
85 # Edge Case: Invalid syntax - even number of tokens (should be odd: number, operation, number, ...)
86 if len(tokens) % 2 == 0:
87 raise ValueError("syntax error")
88
89 # Evaluate from left to right
90 result = tokens[0]
91
92 # Edge Case: First token is not a number
93 if not isinstance(result, int):
94 raise ValueError("syntax error")
95
96 i = 1
97 while i < len(tokens):
98 # Edge Case: Missing operator or operand
99 if i + 1 >= len(tokens):
100 raise ValueError("syntax error")
101
102 operator = tokens[i]
103 operand = tokens[i + 1]
104
105 # Edge Case: Operator is not a string or operand is not an integer
106 if not isinstance(operator, str) or not isinstance(operand, int):
107 raise ValueError("syntax error")
108
109 if operator == 'plus':
110 result += operand
111 elif operator == 'minus':
112 result -= operand
113 elif operator == 'multiplied':
114 result *= operand
115 elif operator == 'divided':
116 # Edge Case: Division by zero
117 if operand == 0:
118 raise ValueError("syntax error") # Following test expectations
119 result //= operand # Integer division
120 else:
121 # Edge Case: Unknown operation during evaluation
122 raise ValueError("unknown operation")
123
124 i += 2
125
126 return result
127 # Handled Edge Cases: Empty string, None input, question not starting with "What is",
128 # question too short, empty expression, negative numbers, consecutive operators,
129 # malformed numbers, unknown operations, expression with just a number,
130 # insufficient tokens, even number of tokens, first token not a number,
131 # missing operator or operand, operator not string or operand not integer,
132 # 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.