| 1 | 1 | def answer(question: str) -> int: |
| 2 | | pass |
| 2 | # Edge Case: Empty or null input strings |
| 3 | if not question: |
| 4 | raise ValueError("syntax error") |
| 5 | |
| 6 | # Edge Case: Questions without proper 'What is' prefix |
| 7 | if not question.startswith("What is"): |
| 8 | raise ValueError("unknown operation") |
| 9 | |
| 10 | # Check if question ends with a question mark |
| 11 | if not question.endswith("?"): |
| 12 | raise ValueError("syntax error") |
| 13 | |
| 14 | # Remove the "What is" prefix and the trailing question mark |
| 15 | expression = question[8:].rstrip('?').strip() |
| 16 | |
| 17 | # Edge Case: Empty expression after removing prefix and question mark |
| 18 | if not expression: |
| 19 | raise ValueError("syntax error") |
| 20 | |
| 21 | # Split the expression into tokens |
| 22 | tokens = expression.split() |
| 23 | |
| 24 | # Edge Case: Only a number is provided |
| 25 | if len(tokens) == 1: |
| 26 | try: |
| 27 | return int(tokens[0]) |
| 28 | except ValueError: |
| 29 | # Edge Case: Non-numeric value provided |
| 30 | raise ValueError("syntax error") |
| 31 | |
| 32 | # Parse the tokens |
| 33 | equation = [] |
| 34 | i = 0 |
| 35 | while i < len(tokens): |
| 36 | token = tokens[i] |
| 37 | if token.lstrip('-').isdigit(): |
| 38 | equation.append(int(token)) |
| 39 | elif token == "plus": |
| 40 | equation.append('+') |
| 41 | elif token == "minus": |
| 42 | equation.append('-') |
| 43 | elif token == "multiplied" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 44 | equation.append('*') |
| 45 | i += 1 # Skip the "by" |
| 46 | elif token == "divided" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 47 | equation.append('/') |
| 48 | i += 1 # Skip the "by" |
| 49 | else: |
| 50 | # Edge Case: Unsupported operations |
| 51 | raise ValueError("unknown operation") |
| 52 | i += 1 |
| 53 | |
| 54 | # Edge Case: Invalid syntax (e.g., 'plus plus', missing numbers) |
| 55 | if len(equation) < 3 or len(equation) % 2 == 0: |
| 56 | raise ValueError("syntax error") |
| 57 | |
| 58 | # Check that the pattern is number, operator, number, ... |
| 59 | for i in range(len(equation)): |
| 60 | if i % 2 == 0: # Even indices should be numbers |
| 61 | if not isinstance(equation[i], int): |
| 62 | raise ValueError("syntax error") |
| 63 | else: # Odd indices should be operators |
| 64 | if equation[i] not in ['+', '-', '*', '/']: |
| 65 | raise ValueError("syntax error") |
| 66 | |
| 67 | # Evaluate the expression from left to right |
| 68 | result = equation[0] |
| 69 | i = 1 |
| 70 | while i < len(equation): |
| 71 | operator = equation[i] |
| 72 | operand = equation[i + 1] |
| 73 | |
| 74 | # Edge Case: Division by zero |
| 75 | if operator == '/' and operand == 0: |
| 76 | raise ValueError("syntax error") |
| 77 | |
| 78 | if operator == '+': |
| 79 | result += operand |
| 80 | elif operator == '-': |
| 81 | result -= operand |
| 82 | elif operator == '*': |
| 83 | result *= operand |
| 84 | elif operator == '/': |
| 85 | result //= operand # Integer division as per examples |
| 86 | |
| 87 | i += 2 |
| 88 | |
| 89 | return result |