who-are-you

Finished
11def answer(question: str) -> int:
2 pass
2 # Edge Case: Empty 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 the "What is " prefix and the trailing question mark
11 expression = question[8:-1].strip()
12
13 # Edge Case: Empty expression after removing prefix and question mark
14 if not expression:
15 raise ValueError("syntax error")
16
17 # Tokenize the expression
18 tokens = []
19 i = 0
20 while i < len(expression):
21 if expression[i].isspace():
22 i += 1
23 continue
24 elif expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()):
25 # Parse negative numbers
26 start = i
27 if expression[i] == '-':
28 i += 1
29 while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'):
30 i += 1
31 # Edge Case: Invalid number format (e.g., decimal points)
32 if '.' in expression[start:i]:
33 raise ValueError("syntax error")
34 tokens.append(int(expression[start:i]))
35 elif expression[i:].startswith('plus'):
36 tokens.append('plus')
37 i += 4
38 elif expression[i:].startswith('minus'):
39 tokens.append('minus')
40 i += 5
41 elif expression[i:].startswith('multiplied by'):
42 tokens.append('multiplied')
43 i += 13
44 elif expression[i:].startswith('divided by'):
45 tokens.append('divided')
46 i += 10
47 else:
48 # Edge Case: Unknown operation
49 raise ValueError("unknown operation")
50
51 # Edge Case: Empty tokens list
52 if not tokens:
53 raise ValueError("syntax error")
54
55 # Edge Case: First token must be a number
56 if not isinstance(tokens[0], int):
57 raise ValueError("syntax error")
58
59 result = tokens[0]
60 i = 1
61
62 # Process operations from left to right
63 while i < len(tokens):
64 # Edge Case: Expecting an operator but found something else
65 if i >= len(tokens) or not isinstance(tokens[i], str):
66 raise ValueError("syntax error")
67
68 op = tokens[i]
69
70 # Edge Case: Expecting a number after operator but not found
71 if i + 1 >= len(tokens) or not isinstance(tokens[i + 1], int):
72 raise ValueError("syntax error")
73
74 operand = tokens[i + 1]
75
76 if op == 'plus':
77 result += operand
78 elif op == 'minus':
79 result -= operand
80 elif op == 'multiplied':
81 result *= operand
82 elif op == 'divided':
83 # Edge Case: Division by zero
84 if operand == 0:
85 raise ValueError("syntax error")
86 result //= operand # Integer division as per examples
87 else:
88 # Edge Case: Unknown operation
89 raise ValueError("unknown operation")
90
91 i += 2
92
93 # Edge Case: Trailing operators
94 if i > len(tokens) and i - 2 < len(tokens) - 1:
95 raise ValueError("syntax error")
96
97 return result
98 # Handled Edge Cases: Empty input, None input, questions not starting with "What is", empty expression after parsing, invalid number formats, unknown operations, first token not being a number, expecting operator but finding something else, expecting number after operator but not found, division by zero, trailing operators
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.