| 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 number (including negative numbers) |
| 26 | start = i |
| 27 | if question[i] == '-': |
| 28 | i += 1 |
| 29 | while i < len(question) and (question[i].isdigit() or question[i] == '.'): |
| 30 | i += 1 |
| 31 | # Edge Case: Invalid number format (e.g., "5.") |
| 32 | if question[start:i].endswith('.'): |
| 33 | raise ValueError("syntax error") |
| 34 | try: |
| 35 | num = int(question[start:i]) |
| 36 | tokens.append(num) |
| 37 | except ValueError: |
| 38 | raise ValueError("syntax error") |
| 39 | elif question[i].isalpha(): |
| 40 | # Parse word (operation) |
| 41 | start = i |
| 42 | while i < len(question) and question[i].isalpha(): |
| 43 | i += 1 |
| 44 | word = question[start:i] |
| 45 | if word == "plus": |
| 46 | tokens.append("+") |
| 47 | elif word == "minus": |
| 48 | tokens.append("-") |
| 49 | elif word == "multiplied" and i < len(question) and question[i:].startswith(" by"): |
| 50 | # Skip "by" |
| 51 | i += 3 |
| 52 | tokens.append("*") |
| 53 | elif word == "divided" and i < len(question) and question[i:].startswith(" by"): |
| 54 | # Skip "by" |
| 55 | i += 3 |
| 56 | tokens.append("/") |
| 57 | else: |
| 58 | # Edge Case: Unknown operation |
| 59 | raise ValueError("unknown operation") |
| 60 | else: |
| 61 | # Edge Case: Invalid character |
| 62 | raise ValueError("syntax error") |
| 63 | |
| 64 | # Edge Case: No tokens |
| 65 | if not tokens: |
| 66 | raise ValueError("syntax error") |
| 67 | |
| 68 | # Edge Case: Expression starts with an operator |
| 69 | if isinstance(tokens[0], str): |
| 70 | raise ValueError("syntax error") |
| 71 | |
| 72 | # Evaluate the expression from left to right |
| 73 | result = tokens[0] |
| 74 | i = 1 |
| 75 | while i < len(tokens): |
| 76 | # Edge Case: Unexpected end of expression |
| 77 | if i + 1 >= len(tokens): |
| 78 | raise ValueError("syntax error") |
| 79 | |
| 80 | # Edge Case: Expected operator but found number |
| 81 | if not isinstance(tokens[i], str): |
| 82 | raise ValueError("syntax error") |
| 83 | |
| 84 | operator = tokens[i] |
| 85 | |
| 86 | # Edge Case: Expected number but found operator |
| 87 | if not isinstance(tokens[i+1], int): |
| 88 | raise ValueError("syntax error") |
| 89 | |
| 90 | operand = tokens[i+1] |
| 91 | |
| 92 | if operator == "+": |
| 93 | result += operand |
| 94 | elif operator == "-": |
| 95 | result -= operand |
| 96 | elif operator == "*": |
| 97 | result *= operand |
| 98 | elif operator == "/": |
| 99 | # Edge Case: Division by zero |
| 100 | if operand == 0: |
| 101 | raise ValueError("syntax error") # Or we could raise ZeroDivisionError |
| 102 | result //= operand # Integer division as per examples |
| 103 | else: |
| 104 | # This should not happen due to earlier validation, but just in case |
| 105 | raise ValueError("unknown operation") |
| 106 | |
| 107 | i += 2 |
| 108 | |
| 109 | return result |
| 110 | # 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 character, No tokens, Expression starts with an operator, Unexpected end of expression, Expected operator but found number, Expected number but found operator, Division by zero |