| 1 | 1 | def answer(question: str) -> int: |
| 2 | | pass |
| 2 | # Edge Case: Empty or None 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 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 | # Handle negative numbers |
| 26 | start = i |
| 27 | i += 1 |
| 28 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 29 | i += 1 |
| 30 | # Edge Case: Non-integer numbers |
| 31 | if '.' in expression[start:i]: |
| 32 | raise ValueError("syntax error") |
| 33 | tokens.append(int(expression[start:i])) |
| 34 | elif expression[i:].startswith("plus"): |
| 35 | tokens.append("+") |
| 36 | i += 4 |
| 37 | elif expression[i:].startswith("minus"): |
| 38 | tokens.append("-") |
| 39 | i += 5 |
| 40 | elif expression[i:].startswith("multiplied by"): |
| 41 | tokens.append("*") |
| 42 | i += 13 |
| 43 | elif expression[i:].startswith("divided by"): |
| 44 | tokens.append("/") |
| 45 | i += 10 |
| 46 | else: |
| 47 | # Edge Case: Unknown operations |
| 48 | raise ValueError("unknown operation") |
| 49 | |
| 50 | # Edge Case: Invalid syntax - empty tokens |
| 51 | if not tokens: |
| 52 | raise ValueError("syntax error") |
| 53 | |
| 54 | # Edge Case: Invalid syntax - expression ending with an operator |
| 55 | if tokens[-1] in ["+", "-", "*", "/"]: |
| 56 | raise ValueError("syntax error") |
| 57 | |
| 58 | # Edge Case: Invalid syntax - expression starting with an operator (except minus for negative numbers) |
| 59 | if tokens[0] in ["+", "*", "/"]: |
| 60 | raise ValueError("syntax error") |
| 61 | |
| 62 | # Evaluate the expression from left to right |
| 63 | result = tokens[0] |
| 64 | |
| 65 | # Edge Case: First token is not a number (except negative numbers already handled) |
| 66 | if not isinstance(result, int): |
| 67 | raise ValueError("syntax error") |
| 68 | |
| 69 | i = 1 |
| 70 | while i < len(tokens): |
| 71 | # Edge Case: Invalid syntax - consecutive operators |
| 72 | if i+1 >= len(tokens) or tokens[i+1] in ["+", "-", "*", "/"]: |
| 73 | raise ValueError("syntax error") |
| 74 | |
| 75 | operator = tokens[i] |
| 76 | operand = tokens[i+1] |
| 77 | |
| 78 | if operator == "+": |
| 79 | result += operand |
| 80 | elif operator == "-": |
| 81 | result -= operand |
| 82 | elif operator == "*": |
| 83 | result *= operand |
| 84 | elif operator == "/": |
| 85 | # Edge Case: Division by zero |
| 86 | if operand == 0: |
| 87 | raise ValueError("syntax error") |
| 88 | result //= operand # Integer division as per examples |
| 89 | else: |
| 90 | # Edge Case: Unknown operator (should not happen with current tokenization) |
| 91 | raise ValueError("unknown operation") |
| 92 | |
| 93 | i += 2 |
| 94 | |
| 95 | return result |
| 96 | # Handled Edge Cases: Empty question, None question, question not starting with "What is", empty expression after parsing, non-integer numbers, unknown operations, expression ending with operator, expression starting with operator, first token not being a number, consecutive operators, division by zero |