| 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] == '-': |
| 25 | # Handle 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: Standalone minus sign (not part of a number) |
| 32 | if question[start:i] == '-': |
| 33 | raise ValueError("syntax error") |
| 34 | try: |
| 35 | num = int(question[start:i]) |
| 36 | tokens.append(num) |
| 37 | except ValueError: |
| 38 | # Edge Case: Invalid number format |
| 39 | raise ValueError("syntax error") |
| 40 | elif question[i].isalpha(): |
| 41 | # Handle words (operations) |
| 42 | start = i |
| 43 | while i < len(question) and question[i].isalpha(): |
| 44 | i += 1 |
| 45 | word = question[start:i] |
| 46 | if word == 'plus': |
| 47 | tokens.append('+') |
| 48 | elif word == 'minus': |
| 49 | tokens.append('-') |
| 50 | elif word == 'multiplied': |
| 51 | # Check for "by" after "multiplied" |
| 52 | if i >= len(question) or not question[i:].startswith(" by"): |
| 53 | raise ValueError("syntax error") |
| 54 | i += 3 # Skip " by" |
| 55 | tokens.append('*') |
| 56 | elif word == 'divided': |
| 57 | # Check for "by" after "divided" |
| 58 | if i >= len(question) or not question[i:].startswith(" by"): |
| 59 | raise ValueError("syntax error") |
| 60 | i += 3 # Skip " by" |
| 61 | tokens.append('/') |
| 62 | else: |
| 63 | # Edge Case: Unknown operation |
| 64 | raise ValueError("unknown operation") |
| 65 | else: |
| 66 | # Edge Case: Invalid character |
| 67 | raise ValueError("syntax error") |
| 68 | |
| 69 | # Edge Case: Empty tokens |
| 70 | if not tokens: |
| 71 | raise ValueError("syntax error") |
| 72 | |
| 73 | # Edge Case: First token is not a number |
| 74 | if not isinstance(tokens[0], int): |
| 75 | raise ValueError("syntax error") |
| 76 | |
| 77 | # Evaluate the expression from left to right |
| 78 | result = tokens[0] |
| 79 | i = 1 |
| 80 | |
| 81 | while i < len(tokens): |
| 82 | # Edge Case: Expecting an operator but reached end of tokens |
| 83 | if i >= len(tokens): |
| 84 | raise ValueError("syntax error") |
| 85 | |
| 86 | # Edge Case: Expecting an operator but got a number |
| 87 | if not isinstance(tokens[i], str): |
| 88 | raise ValueError("syntax error") |
| 89 | |
| 90 | operator = tokens[i] |
| 91 | i += 1 |
| 92 | |
| 93 | # Edge Case: Expecting a number but reached end of tokens |
| 94 | if i >= len(tokens): |
| 95 | raise ValueError("syntax error") |
| 96 | |
| 97 | # Edge Case: Expecting a number but got an operator |
| 98 | if not isinstance(tokens[i], int): |
| 99 | raise ValueError("syntax error") |
| 100 | |
| 101 | operand = tokens[i] |
| 102 | i += 1 |
| 103 | |
| 104 | if operator == '+': |
| 105 | result += operand |
| 106 | elif operator == '-': |
| 107 | result -= operand |
| 108 | elif operator == '*': |
| 109 | result *= operand |
| 110 | elif operator == '/': |
| 111 | # Edge Case: Division by zero |
| 112 | if operand == 0: |
| 113 | raise ValueError("syntax error") # Or we could raise ZeroDivisionError |
| 114 | result //= operand # Integer division as per examples |
| 115 | else: |
| 116 | # Edge Case: Unknown operator (should not happen with current parsing) |
| 117 | raise ValueError("unknown operation") |
| 118 | |
| 119 | return result |
| 120 | # Handled Edge Cases: Empty question, Question doesn't start with "What is", Empty expression after removing "What is" and "?", Standalone minus sign, Invalid number format, Unknown operation, Invalid character, Empty tokens, First token is not a number, Expecting an operator but reached end of tokens, Expecting an operator but got a number, Expecting a number but reached end of tokens, Expecting a number but got an operator, Division by zero, Unknown operator |