| 1 | 1 | def answer(question: str) -> int: |
| 2 | | pass |
| 2 | # Edge Case: Non-math questions |
| 3 | if not question.startswith("What is"): |
| 4 | raise ValueError("syntax error") |
| 5 | |
| 6 | # Remove the "What is " prefix and the trailing "?" |
| 7 | expression = question[8:-1].strip() |
| 8 | |
| 9 | # Edge Case: Empty expression after "What is" |
| 10 | if not expression: |
| 11 | raise ValueError("syntax error") |
| 12 | |
| 13 | # Split the expression into tokens |
| 14 | tokens = expression.split() |
| 15 | |
| 16 | # Edge Case: Expression with no tokens |
| 17 | if not tokens: |
| 18 | raise ValueError("syntax error") |
| 19 | |
| 20 | # Parse the first number |
| 21 | try: |
| 22 | result = int(tokens[0]) |
| 23 | tokens = tokens[1:] |
| 24 | except ValueError: |
| 25 | # Edge Case: First token is not a number |
| 26 | raise ValueError("syntax error") |
| 27 | |
| 28 | # Process operations |
| 29 | while tokens: |
| 30 | # Edge Case: Incomplete operation (missing operator or operand) |
| 31 | if len(tokens) < 1: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | operator = tokens[0] |
| 35 | |
| 36 | # Handle two-word operations first |
| 37 | if operator == "multiplied" or operator == "divided": |
| 38 | # Need at least one more token for "by" |
| 39 | if len(tokens) < 2: |
| 40 | raise ValueError("syntax error") |
| 41 | |
| 42 | # Check if the second token is "by" |
| 43 | if tokens[1] != "by": |
| 44 | raise ValueError("unknown operation") |
| 45 | |
| 46 | # Need a third token for the operand |
| 47 | if len(tokens) < 3: |
| 48 | raise ValueError("syntax error") |
| 49 | |
| 50 | try: |
| 51 | operand = int(tokens[2]) |
| 52 | except ValueError: |
| 53 | # Check if the token might be an unknown operation |
| 54 | if tokens[2] in ["plus", "minus", "multiplied", "divided"]: |
| 55 | # This is a syntax error - operator where operand expected |
| 56 | raise ValueError("syntax error") |
| 57 | else: |
| 58 | # This might be an unknown operation |
| 59 | raise ValueError("unknown operation") |
| 60 | |
| 61 | # Perform the operation |
| 62 | if operator == "multiplied": |
| 63 | result *= operand |
| 64 | else: # operator == "divided" |
| 65 | result //= operand |
| 66 | |
| 67 | # Move to the next operation (consume 3 tokens: operator, "by", operand) |
| 68 | tokens = tokens[3:] |
| 69 | else: |
| 70 | # Handle single-word operations |
| 71 | # Check if operator is valid first |
| 72 | if operator not in ["plus", "minus"]: |
| 73 | # Edge Case: Unknown operation |
| 74 | raise ValueError("unknown operation") |
| 75 | |
| 76 | # Need at least one more token for the operand |
| 77 | if len(tokens) < 2: |
| 78 | raise ValueError("syntax error") |
| 79 | |
| 80 | try: |
| 81 | operand = int(tokens[1]) |
| 82 | except ValueError: |
| 83 | # Check if the token might be an unknown operation |
| 84 | if tokens[1] in ["plus", "minus", "multiplied", "divided"]: |
| 85 | # This is a syntax error - operator where operand expected |
| 86 | raise ValueError("syntax error") |
| 87 | else: |
| 88 | # This might be an unknown operation |
| 89 | raise ValueError("unknown operation") |
| 90 | |
| 91 | # Perform the operation |
| 92 | if operator == "plus": |
| 93 | result += operand |
| 94 | elif operator == "minus": |
| 95 | result -= operand |
| 96 | |
| 97 | # Move to the next operation (consume 2 tokens: operator, operand) |
| 98 | tokens = tokens[2:] |
| 99 | |
| 100 | return result |
| 101 | # Handled Edge Cases: Non-math questions, Empty expression, Expression with no tokens, First token is not a number, Incomplete operation, Operand is not a number, Unknown operation |