| 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" and "?" from the question |
| 11 | question = question[8:-1].strip() |
| 12 | |
| 13 | # Edge Case: Empty expression after removing "What is" and "?" |
| 14 | if not question: |
| 15 | raise ValueError("syntax error") |
| 16 | |
| 17 | # Tokenize the question |
| 18 | tokens = [] |
| 19 | i = 0 |
| 20 | while i < len(question): |
| 21 | if question[i].isspace(): |
| 22 | i += 1 |
| 23 | continue |
| 24 | elif question[i].isdigit() or (question[i] == '-' and i < len(question) - 1 and question[i+1].isdigit()): |
| 25 | # Parse negative numbers |
| 26 | start = i |
| 27 | i += 1 |
| 28 | while i < len(question) and (question[i].isdigit() or question[i] == '.'): |
| 29 | i += 1 |
| 30 | # Edge Case: Invalid number format (e.g., "5.") |
| 31 | if question[start:i].endswith('.'): |
| 32 | raise ValueError("syntax error") |
| 33 | try: |
| 34 | num = int(question[start:i]) |
| 35 | tokens.append(num) |
| 36 | except ValueError: |
| 37 | raise ValueError("syntax error") |
| 38 | elif question[i:].startswith("plus"): |
| 39 | tokens.append("+") |
| 40 | i += 4 |
| 41 | elif question[i:].startswith("minus"): |
| 42 | tokens.append("-") |
| 43 | i += 5 |
| 44 | elif question[i:].startswith("multiplied by"): |
| 45 | tokens.append("*") |
| 46 | i += 13 |
| 47 | elif question[i:].startswith("divided by"): |
| 48 | tokens.append("/") |
| 49 | i += 10 |
| 50 | else: |
| 51 | # Edge Case: Unknown operation |
| 52 | raise ValueError("unknown operation") |
| 53 | |
| 54 | # Edge Case: Invalid syntax (e.g., ends with an operator) |
| 55 | if not tokens: |
| 56 | raise ValueError("syntax error") |
| 57 | |
| 58 | # Edge Case: Expression starts with an operator |
| 59 | if isinstance(tokens[0], str): |
| 60 | raise ValueError("syntax error") |
| 61 | |
| 62 | # Evaluate the expression from left to right |
| 63 | result = tokens[0] |
| 64 | i = 1 |
| 65 | while i < len(tokens): |
| 66 | # Edge Case: Invalid syntax (e.g., two operators in a row) |
| 67 | if i + 1 >= len(tokens) or isinstance(tokens[i+1], str): |
| 68 | raise ValueError("syntax error") |
| 69 | |
| 70 | operator = tokens[i] |
| 71 | operand = tokens[i+1] |
| 72 | |
| 73 | if operator == "+": |
| 74 | result += operand |
| 75 | elif operator == "-": |
| 76 | result -= operand |
| 77 | elif operator == "*": |
| 78 | result *= operand |
| 79 | elif operator == "/": |
| 80 | # Edge Case: Division by zero |
| 81 | if operand == 0: |
| 82 | raise ValueError("syntax error") |
| 83 | result //= operand # Integer division as per examples |
| 84 | else: |
| 85 | # Edge Case: Unknown operation (should not happen with current tokenization) |
| 86 | raise ValueError("unknown operation") |
| 87 | |
| 88 | i += 2 |
| 89 | |
| 90 | return result |
| 91 | # Handled Edge Cases: Empty question, Question doesn't start with "What is", Empty expression after removing "What is" and "?", Invalid number format, Unknown operation, Invalid syntax (ends with operator), Expression starts with operator, Two operators in a row, Division by zero |