| 1 | 1 | def 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 the "What is " prefix and the question mark at the end |
| 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: Invalid syntax - consecutive operators or operators at start/end |
| 26 | while i < len(expression): |
| 27 | if expression[i].isspace(): |
| 28 | i += 1 |
| 29 | continue |
| 30 | |
| 31 | # Parse numbers (including negative numbers) |
| 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 | if '.' in expression[start:i]: |
| 40 | raise ValueError("syntax error") |
| 41 | tokens.append(int(expression[start:i])) |
| 42 | # Parse operators |
| 43 | elif expression[i:].startswith('plus'): |
| 44 | tokens.append('plus') |
| 45 | i += 4 |
| 46 | elif expression[i:].startswith('minus'): |
| 47 | tokens.append('minus') |
| 48 | i += 5 |
| 49 | elif expression[i:].startswith('multiplied by'): |
| 50 | tokens.append('multiplied') |
| 51 | i += 13 |
| 52 | elif expression[i:].startswith('divided by'): |
| 53 | tokens.append('divided') |
| 54 | i += 10 |
| 55 | else: |
| 56 | # Edge Case: Unknown operations |
| 57 | raise ValueError("unknown operation") |
| 58 | |
| 59 | # Edge Case: Empty tokens list |
| 60 | if not tokens: |
| 61 | raise ValueError("syntax error") |
| 62 | |
| 63 | # Edge Case: Expression starts with an operator |
| 64 | if isinstance(tokens[0], str): |
| 65 | raise ValueError("syntax error") |
| 66 | |
| 67 | # Evaluate the expression from left to right |
| 68 | result = tokens[0] |
| 69 | i = 1 |
| 70 | |
| 71 | # Edge Case: Odd number of tokens (missing operator or operand) |
| 72 | if len(tokens) % 2 == 0: |
| 73 | raise ValueError("syntax error") |
| 74 | |
| 75 | while i < len(tokens): |
| 76 | # Edge Case: Missing operator between numbers |
| 77 | if not isinstance(tokens[i], str): |
| 78 | raise ValueError("syntax error") |
| 79 | |
| 80 | # Edge Case: Missing operand after operator |
| 81 | if i + 1 >= len(tokens) or isinstance(tokens[i + 1], str): |
| 82 | raise ValueError("syntax error") |
| 83 | |
| 84 | operator = tokens[i] |
| 85 | operand = tokens[i + 1] |
| 86 | |
| 87 | if operator == 'plus': |
| 88 | result += operand |
| 89 | elif operator == 'minus': |
| 90 | result -= operand |
| 91 | elif operator == 'multiplied': |
| 92 | result *= operand |
| 93 | elif operator == 'divided': |
| 94 | # Edge Case: Division by zero |
| 95 | if operand == 0: |
| 96 | raise ValueError("syntax error") |
| 97 | result //= operand # Integer division as per examples |
| 98 | else: |
| 99 | # Edge Case: Unknown operation (should not happen with current parsing) |
| 100 | raise ValueError("unknown operation") |
| 101 | |
| 102 | i += 2 |
| 103 | |
| 104 | return result |
| 105 | # Handled Edge Cases: Empty string, None input, question not starting with "What is", question too short, |
| 106 | # empty expression, non-integer numbers, unknown operations, empty tokens list, expression starting with operator, |
| 107 | # odd number of tokens, missing operators, missing operands, division by zero |