| 1 | 1 | def answer(question: str) -> int: |
| 2 | | pass |
| 2 | # Edge Case: Empty or whitespace-only input |
| 3 | if not question or not question.strip(): |
| 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 | # Extract the expression part after "What is" and before "?" |
| 11 | # Edge Case: Missing "?" at the end |
| 12 | if not question.endswith("?"): |
| 13 | raise ValueError("syntax error") |
| 14 | |
| 15 | expression = question[8:-1].strip() |
| 16 | |
| 17 | # Edge Case: Empty expression after "What is" |
| 18 | if not expression: |
| 19 | raise ValueError("syntax error") |
| 20 | |
| 21 | # Replace word operations with symbols for easier parsing |
| 22 | expression = expression.replace("plus", "+") |
| 23 | expression = expression.replace("minus", "-") |
| 24 | expression = expression.replace("multiplied by", "*") |
| 25 | expression = expression.replace("divided by", "/") |
| 26 | |
| 27 | # Tokenize the expression |
| 28 | tokens = [] |
| 29 | i = 0 |
| 30 | while i < len(expression): |
| 31 | if expression[i].isspace(): |
| 32 | i += 1 |
| 33 | continue |
| 34 | elif expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()): |
| 35 | # Handle negative numbers |
| 36 | start = i |
| 37 | if expression[i] == '-': |
| 38 | i += 1 |
| 39 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 40 | i += 1 |
| 41 | # Edge Case: Numbers with decimal points (invalid for this problem) |
| 42 | if '.' in expression[start:i]: |
| 43 | raise ValueError("syntax error") |
| 44 | tokens.append(int(expression[start:i])) |
| 45 | elif expression[i] in '+-*/': |
| 46 | tokens.append(expression[i]) |
| 47 | i += 1 |
| 48 | else: |
| 49 | # Edge Case: Unknown operations or invalid characters |
| 50 | # Check if it's a valid operation word that wasn't replaced |
| 51 | word_start = i |
| 52 | while i < len(expression) and not expression[i].isspace(): |
| 53 | i += 1 |
| 54 | word = expression[word_start:i] |
| 55 | if word in ["plus", "minus", "multiplied", "divided"]: |
| 56 | # Edge Case: Incomplete operation words |
| 57 | if word == "multiplied" or word == "divided": |
| 58 | raise ValueError("syntax error") |
| 59 | # These should have been replaced, so this is an error |
| 60 | raise ValueError("syntax error") |
| 61 | else: |
| 62 | raise ValueError("unknown operation") |
| 63 | |
| 64 | # Edge Case: Expression with just a number |
| 65 | if len(tokens) == 1 and isinstance(tokens[0], int): |
| 66 | return tokens[0] |
| 67 | |
| 68 | # Edge Case: Even number of tokens (should be odd: number, operator, number, ...) |
| 69 | if len(tokens) % 2 == 0: |
| 70 | raise ValueError("syntax error") |
| 71 | |
| 72 | # Edge Case: Expression starting or ending with an operator |
| 73 | if len(tokens) > 0 and (isinstance(tokens[0], str) or isinstance(tokens[-1], str)): |
| 74 | raise ValueError("syntax error") |
| 75 | |
| 76 | # Evaluate from left to right |
| 77 | result = tokens[0] |
| 78 | i = 1 |
| 79 | while i < len(tokens): |
| 80 | # Edge Case: Missing operator between numbers |
| 81 | if not isinstance(tokens[i], str): |
| 82 | raise ValueError("syntax error") |
| 83 | |
| 84 | operator = tokens[i] |
| 85 | # Edge Case: Missing operand after operator |
| 86 | if i + 1 >= len(tokens): |
| 87 | raise ValueError("syntax error") |
| 88 | |
| 89 | # Edge Case: Non-numeric operand |
| 90 | if not isinstance(tokens[i + 1], int): |
| 91 | raise ValueError("syntax error") |
| 92 | |
| 93 | operand = tokens[i + 1] |
| 94 | |
| 95 | if operator == "+": |
| 96 | result += operand |
| 97 | elif operator == "-": |
| 98 | result -= operand |
| 99 | elif operator == "*": |
| 100 | result *= operand |
| 101 | elif operator == "/": |
| 102 | # Edge Case: Division by zero |
| 103 | if operand == 0: |
| 104 | raise ValueError("syntax error") |
| 105 | result //= operand # Integer division as per examples |
| 106 | else: |
| 107 | # Edge Case: Unsupported operation symbol |
| 108 | raise ValueError("unknown operation") |
| 109 | |
| 110 | i += 2 |
| 111 | |
| 112 | return result |
| 113 | |
| 114 | # Handled Edge Cases: Empty input, whitespace-only input, missing "What is", missing "?", |
| 115 | # empty expression, unknown operations, invalid characters, decimal numbers, |
| 116 | # expression with just a number, even number of tokens, expression starting/ending with operator, |
| 117 | # missing operator between numbers, missing operand after operator, non-numeric operand, |
| 118 | # division by zero, unsupported operation symbols, incomplete operation words |