| 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 is just "What is?" |
| 12 | if question == "What is?": |
| 13 | raise ValueError("syntax error") |
| 14 | |
| 15 | content = question[8:-1].strip() |
| 16 | |
| 17 | # Edge Case: Empty content after "What is" |
| 18 | if not content: |
| 19 | raise ValueError("syntax error") |
| 20 | |
| 21 | # Tokenize the content |
| 22 | tokens = [] |
| 23 | i = 0 |
| 24 | |
| 25 | while i < len(content): |
| 26 | # Skip whitespace |
| 27 | if content[i].isspace(): |
| 28 | i += 1 |
| 29 | continue |
| 30 | |
| 31 | # Parse numbers (including negative) |
| 32 | if content[i].isdigit() or (content[i] == '-' and i < len(content) - 1 and content[i+1].isdigit()): |
| 33 | start = i |
| 34 | if content[i] == '-': |
| 35 | i += 1 |
| 36 | while i < len(content) and (content[i].isdigit() or content[i] == '-'): |
| 37 | if content[i] == '-': |
| 38 | # Edge Case: Negative sign in the middle of a number |
| 39 | if i > start: |
| 40 | raise ValueError("syntax error") |
| 41 | i += 1 |
| 42 | tokens.append(int(content[start:i])) |
| 43 | continue |
| 44 | |
| 45 | # Parse operations |
| 46 | if content[i:].startswith("plus"): |
| 47 | tokens.append("+") |
| 48 | i += 4 |
| 49 | continue |
| 50 | elif content[i:].startswith("minus"): |
| 51 | tokens.append("-") |
| 52 | i += 5 |
| 53 | continue |
| 54 | elif content[i:].startswith("multiplied by"): |
| 55 | tokens.append("*") |
| 56 | i += 13 |
| 57 | continue |
| 58 | elif content[i:].startswith("divided by"): |
| 59 | tokens.append("/") |
| 60 | i += 10 |
| 61 | continue |
| 62 | else: |
| 63 | # Edge Case: Unknown operation |
| 64 | raise ValueError("unknown operation") |
| 65 | |
| 66 | # Edge Case: No tokens |
| 67 | if not tokens: |
| 68 | raise ValueError("syntax error") |
| 69 | |
| 70 | # Edge Case: First token is not a number |
| 71 | if not isinstance(tokens[0], int): |
| 72 | raise ValueError("syntax error") |
| 73 | |
| 74 | result = tokens[0] |
| 75 | i = 1 |
| 76 | |
| 77 | # Process operations |
| 78 | while i < len(tokens): |
| 79 | # Edge Case: Not enough tokens for operation |
| 80 | if i + 1 >= len(tokens): |
| 81 | raise ValueError("syntax error") |
| 82 | |
| 83 | operation = tokens[i] |
| 84 | operand = tokens[i + 1] |
| 85 | |
| 86 | # Edge Case: Operation is not an operator |
| 87 | if not isinstance(operation, str): |
| 88 | raise ValueError("syntax error") |
| 89 | |
| 90 | # Edge Case: Operand is not a number |
| 91 | if not isinstance(operand, int): |
| 92 | raise ValueError("syntax error") |
| 93 | |
| 94 | if operation == "+": |
| 95 | result += operand |
| 96 | elif operation == "-": |
| 97 | result -= operand |
| 98 | elif operation == "*": |
| 99 | result *= operand |
| 100 | elif operation == "/": |
| 101 | # Edge Case: Division by zero |
| 102 | if operand == 0: |
| 103 | raise ValueError("syntax error") |
| 104 | result //= operand # Integer division as per examples |
| 105 | else: |
| 106 | # Edge Case: Unknown operation |
| 107 | raise ValueError("unknown operation") |
| 108 | |
| 109 | i += 2 |
| 110 | |
| 111 | return result |
| 112 | # Handled Edge Cases: Empty question, Question doesn't start with "What is", Question is just "What is?", |
| 113 | # Empty content after "What is", Negative sign in the middle of a number, Unknown operation, |
| 114 | # No tokens, First token is not a number, Not enough tokens for operation, Operation is not an operator, |
| 115 | # Operand is not a number, Division by zero |