| 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 | # Remove prefix and suffix |
| 16 | expression = question[8:-1].strip() |
| 17 | |
| 18 | # Edge Case: Empty expression after removing prefix/suffix |
| 19 | if not expression: |
| 20 | raise ValueError("syntax error") |
| 21 | |
| 22 | # Tokenize the expression |
| 23 | tokens = [] |
| 24 | i = 0 |
| 25 | |
| 26 | # Edge Case: Malformed expression starting with an operation |
| 27 | # This will be caught during parsing |
| 28 | |
| 29 | while i < len(expression): |
| 30 | # Skip whitespace |
| 31 | if expression[i].isspace(): |
| 32 | i += 1 |
| 33 | continue |
| 34 | |
| 35 | # Parse numbers (including negative) |
| 36 | if expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()): |
| 37 | start = i |
| 38 | if expression[i] == '-': |
| 39 | i += 1 |
| 40 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '-'): |
| 41 | if expression[i] == '-': |
| 42 | # If we find a minus sign not at the start, it's likely an operator |
| 43 | if i > start: |
| 44 | break |
| 45 | i += 1 |
| 46 | num_str = expression[start:i] |
| 47 | # Edge Case: Invalid number format |
| 48 | try: |
| 49 | tokens.append(int(num_str)) |
| 50 | except ValueError: |
| 51 | raise ValueError("syntax error") |
| 52 | continue |
| 53 | |
| 54 | # Parse words (operations) |
| 55 | if expression[i].isalpha(): |
| 56 | start = i |
| 57 | while i < len(expression) and expression[i].isalpha(): |
| 58 | i += 1 |
| 59 | word = expression[start:i] |
| 60 | # Handle multi-word operations |
| 61 | if word == "plus": |
| 62 | tokens.append("+") |
| 63 | elif word == "minus": |
| 64 | tokens.append("-") |
| 65 | elif word == "multiplied" and i < len(expression) and expression[i:].startswith(" by"): |
| 66 | # Skip " by" |
| 67 | i += 3 |
| 68 | tokens.append("*") |
| 69 | elif word == "divided" and i < len(expression) and expression[i:].startswith(" by"): |
| 70 | # Skip " by" |
| 71 | i += 3 |
| 72 | tokens.append("/") |
| 73 | else: |
| 74 | # Edge Case: Unknown operation |
| 75 | raise ValueError("unknown operation") |
| 76 | continue |
| 77 | |
| 78 | # Edge Case: Invalid character |
| 79 | raise ValueError("syntax error") |
| 80 | |
| 81 | # Edge Case: Empty tokens |
| 82 | if not tokens: |
| 83 | raise ValueError("syntax error") |
| 84 | |
| 85 | # Evaluate the expression from left to right |
| 86 | # Edge Case: Expression starts with an operator |
| 87 | if isinstance(tokens[0], str): |
| 88 | raise ValueError("syntax error") |
| 89 | |
| 90 | result = tokens[0] |
| 91 | i = 1 |
| 92 | |
| 93 | # Edge Case: Odd number of tokens (number followed by operator with no second number) |
| 94 | while i < len(tokens): |
| 95 | # We expect an operator |
| 96 | if not isinstance(tokens[i], str): |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | operator = tokens[i] |
| 100 | i += 1 |
| 101 | |
| 102 | # Edge Case: Missing operand after operator |
| 103 | if i >= len(tokens): |
| 104 | raise ValueError("syntax error") |
| 105 | |
| 106 | # We expect a number |
| 107 | if not isinstance(tokens[i], int): |
| 108 | raise ValueError("syntax error") |
| 109 | |
| 110 | operand = tokens[i] |
| 111 | i += 1 |
| 112 | |
| 113 | if operator == "+": |
| 114 | result += operand |
| 115 | elif operator == "-": |
| 116 | result -= operand |
| 117 | elif operator == "*": |
| 118 | result *= operand |
| 119 | elif operator == "/": |
| 120 | # Edge Case: Division by zero |
| 121 | if operand == 0: |
| 122 | raise ValueError("syntax error") # Following test expectations |
| 123 | result //= operand # Integer division as per examples |
| 124 | |
| 125 | # Edge Case: Expression ends with an operator (handled by missing operand check above) |
| 126 | |
| 127 | return result |
| 128 | # Handled Edge Cases: Empty question, Question doesn't start with "What is", Question is just "What is?", |
| 129 | # Empty expression, Invalid number format, Unknown operation, Invalid character, Empty tokens, |
| 130 | # Expression starts with an operator, Odd number of tokens, Missing operand after operator, |
| 131 | # Non-number operand, Division by zero |