| 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 | 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 words (operations) |
| 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": |
| 50 | # Check for "by" after "multiplied" |
| 51 | if i >= len(question) or not question[i:].startswith(" by"): |
| 52 | raise ValueError("syntax error") |
| 53 | i += 3 # Skip " by" |
| 54 | tokens.append("*") |
| 55 | elif word == "divided": |
| 56 | # Check for "by" after "divided" |
| 57 | if i >= len(question) or not question[i:].startswith(" by"): |
| 58 | raise ValueError("syntax error") |
| 59 | i += 3 # Skip " by" |
| 60 | tokens.append("/") |
| 61 | else: |
| 62 | # Edge Case: Unknown operation |
| 63 | raise ValueError("unknown operation") |
| 64 | else: |
| 65 | # Edge Case: Invalid character |
| 66 | raise ValueError("syntax error") |
| 67 | |
| 68 | # Edge Case: No tokens |
| 69 | if not tokens: |
| 70 | raise ValueError("syntax error") |
| 71 | |
| 72 | # Edge Case: First token is not a number |
| 73 | if not isinstance(tokens[0], int): |
| 74 | raise ValueError("syntax error") |
| 75 | |
| 76 | # Evaluate the expression from left to right |
| 77 | result = tokens[0] |
| 78 | i = 1 |
| 79 | |
| 80 | while i < len(tokens): |
| 81 | # Edge Case: Missing operator |
| 82 | if i >= len(tokens) - 1: |
| 83 | raise ValueError("syntax error") |
| 84 | |
| 85 | # Edge Case: Current token is not an operator |
| 86 | if not isinstance(tokens[i], str): |
| 87 | raise ValueError("syntax error") |
| 88 | |
| 89 | operator = tokens[i] |
| 90 | |
| 91 | # Edge Case: Next token is not a number |
| 92 | if not isinstance(tokens[i+1], int): |
| 93 | raise ValueError("syntax error") |
| 94 | |
| 95 | operand = tokens[i+1] |
| 96 | |
| 97 | if operator == "+": |
| 98 | result += operand |
| 99 | elif operator == "-": |
| 100 | result -= operand |
| 101 | elif operator == "*": |
| 102 | result *= operand |
| 103 | elif operator == "/": |
| 104 | # Edge Case: Division by zero |
| 105 | if operand == 0: |
| 106 | raise ValueError("syntax error") |
| 107 | result //= operand # Integer division as per examples |
| 108 | else: |
| 109 | # This should not happen due to earlier validation |
| 110 | raise ValueError("unknown operation") |
| 111 | |
| 112 | i += 2 |
| 113 | |
| 114 | return result |
| 115 | # 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, First token is not a number, Missing operator, Current token is not an operator, Next token is not a number, Division by zero |