| 1 | 1 | def 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 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 | num_str += expression[i] |
| 32 | i += 1 |
| 33 | # Edge Case: Standalone minus without number |
| 34 | if num_str == '-': |
| 35 | raise ValueError("syntax error") |
| 36 | tokens.append(int(num_str)) |
| 37 | # Skip whitespace |
| 38 | while i < len(expression) and expression[i] == ' ': |
| 39 | i += 1 |
| 40 | |
| 41 | while i < len(expression): |
| 42 | if expression[i] == ' ': |
| 43 | i += 1 |
| 44 | continue |
| 45 | |
| 46 | # Parse numbers |
| 47 | if expression[i].isdigit() or (expression[i] == '-' and (i == 0 or expression[i-1] == ' ')): |
| 48 | num_str = '' |
| 49 | # Handle negative numbers |
| 50 | if expression[i] == '-': |
| 51 | num_str = '-' |
| 52 | i += 1 |
| 53 | |
| 54 | # Edge Case: Standalone minus |
| 55 | if num_str == '-' and (i >= len(expression) or not expression[i].isdigit()): |
| 56 | raise ValueError("syntax error") |
| 57 | |
| 58 | while i < len(expression) and expression[i].isdigit(): |
| 59 | num_str += expression[i] |
| 60 | i += 1 |
| 61 | |
| 62 | # Edge Case: Number with trailing decimal point |
| 63 | if i < len(expression) and expression[i] == '.': |
| 64 | raise ValueError("syntax error") |
| 65 | |
| 66 | tokens.append(int(num_str)) |
| 67 | # Parse operators |
| 68 | elif expression[i:].startswith('plus'): |
| 69 | tokens.append('plus') |
| 70 | i += 4 |
| 71 | elif expression[i:].startswith('minus'): |
| 72 | tokens.append('minus') |
| 73 | i += 5 |
| 74 | elif expression[i:].startswith('multiplied by'): |
| 75 | tokens.append('multiplied') |
| 76 | i += 13 |
| 77 | elif expression[i:].startswith('divided by'): |
| 78 | tokens.append('divided') |
| 79 | i += 10 |
| 80 | else: |
| 81 | # Edge Case: Unknown operation |
| 82 | raise ValueError("unknown operation") |
| 83 | |
| 84 | # Edge Case: Empty tokens |
| 85 | if not tokens: |
| 86 | raise ValueError("syntax error") |
| 87 | |
| 88 | # Edge Case: Expression ending with operator |
| 89 | if tokens[-1] in ['plus', 'minus', 'multiplied', 'divided']: |
| 90 | raise ValueError("syntax error") |
| 91 | |
| 92 | # Edge Case: Expression starting with operator |
| 93 | if tokens[0] in ['plus', 'minus', 'multiplied', 'divided']: |
| 94 | raise ValueError("syntax error") |
| 95 | |
| 96 | # Evaluate the expression from left to right |
| 97 | # Edge Case: Single number |
| 98 | if len(tokens) == 1: |
| 99 | if isinstance(tokens[0], int): |
| 100 | return tokens[0] |
| 101 | else: |
| 102 | raise ValueError("syntax error") |
| 103 | |
| 104 | # Edge Case: Invalid sequence (two numbers or two operators in a row) |
| 105 | for i in range(len(tokens) - 1): |
| 106 | if isinstance(tokens[i], int) == isinstance(tokens[i+1], int): |
| 107 | raise ValueError("syntax error") |
| 108 | if tokens[i] in ['plus', 'minus', 'multiplied', 'divided'] and tokens[i+1] in ['plus', 'minus', 'multiplied', 'divided']: |
| 109 | raise ValueError("syntax error") |
| 110 | |
| 111 | result = tokens[0] |
| 112 | i = 1 |
| 113 | |
| 114 | while i < len(tokens): |
| 115 | # Edge Case: Missing operator or operand |
| 116 | if i + 1 >= len(tokens): |
| 117 | raise ValueError("syntax error") |
| 118 | |
| 119 | operator = tokens[i] |
| 120 | operand = tokens[i+1] |
| 121 | |
| 122 | # Edge Case: Non-integer operand |
| 123 | if not isinstance(operand, int): |
| 124 | raise ValueError("syntax error") |
| 125 | |
| 126 | if operator == 'plus': |
| 127 | result += operand |
| 128 | elif operator == 'minus': |
| 129 | result -= operand |
| 130 | elif operator == 'multiplied': |
| 131 | result *= operand |
| 132 | elif operator == 'divided': |
| 133 | # Edge Case: Division by zero |
| 134 | if operand == 0: |
| 135 | raise ValueError("syntax error") |
| 136 | result //= operand # Integer division as per examples |
| 137 | else: |
| 138 | # Edge Case: Unknown operator (should not happen with current parsing) |
| 139 | raise ValueError("unknown operation") |
| 140 | |
| 141 | i += 2 |
| 142 | |
| 143 | return result |
| 144 | # Handled Edge Cases: Empty question, question not starting with "What is", empty expression after parsing, |
| 145 | # standalone minus, number with trailing decimal, unknown operations, empty tokens, expression ending/starting with operator, |
| 146 | # single number, invalid sequences, missing operator/operand, non-integer operand, division by zero |