| 1 | 1 | def answer(question: str) -> int: |
| 2 | | pass |
| 2 | """ |
| 3 | Parse and evaluate a simple math word problem. |
| 4 | |
| 5 | Args: |
| 6 | question: A string representing a math word problem |
| 7 | |
| 8 | Returns: |
| 9 | int: The result of the mathematical operation |
| 10 | |
| 11 | Raises: |
| 12 | ValueError: If the question is malformed, contains unsupported operations, |
| 13 | or has invalid syntax |
| 14 | """ |
| 15 | # Edge Case: Handle non-math questions |
| 16 | if not question.startswith("What is"): |
| 17 | raise ValueError("unknown operation") |
| 18 | |
| 19 | # Remove the "What is" prefix and the question mark |
| 20 | expression = question[8:].rstrip('?').strip() |
| 21 | |
| 22 | # Edge Case: Handle empty expression after "What is" |
| 23 | if not expression: |
| 24 | raise ValueError("syntax error") |
| 25 | |
| 26 | # Split the expression into tokens |
| 27 | tokens = expression.split() |
| 28 | |
| 29 | # Edge Case: Handle single number |
| 30 | if len(tokens) == 1: |
| 31 | try: |
| 32 | return int(tokens[0]) |
| 33 | except ValueError: |
| 34 | raise ValueError("syntax error") |
| 35 | |
| 36 | # Parse the expression |
| 37 | try: |
| 38 | result = int(tokens[0]) |
| 39 | except ValueError: |
| 40 | raise ValueError("syntax error") |
| 41 | |
| 42 | i = 1 |
| 43 | while i < len(tokens): |
| 44 | # Edge Case: Handle operation tokens |
| 45 | if tokens[i] == "plus": |
| 46 | if i + 1 >= len(tokens): |
| 47 | raise ValueError("syntax error") |
| 48 | try: |
| 49 | result += int(tokens[i + 1]) |
| 50 | except ValueError: |
| 51 | raise ValueError("syntax error") |
| 52 | i += 2 |
| 53 | elif tokens[i] == "minus": |
| 54 | if i + 1 >= len(tokens): |
| 55 | raise ValueError("syntax error") |
| 56 | try: |
| 57 | result -= int(tokens[i + 1]) |
| 58 | except ValueError: |
| 59 | raise ValueError("syntax error") |
| 60 | i += 2 |
| 61 | elif tokens[i] == "multiplied" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 62 | if i + 2 >= len(tokens): |
| 63 | raise ValueError("syntax error") |
| 64 | try: |
| 65 | result *= int(tokens[i + 2]) |
| 66 | except ValueError: |
| 67 | raise ValueError("syntax error") |
| 68 | i += 3 |
| 69 | elif tokens[i] == "divided" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 70 | if i + 2 >= len(tokens): |
| 71 | raise ValueError("syntax error") |
| 72 | try: |
| 73 | divisor = int(tokens[i + 2]) |
| 74 | if divisor == 0: |
| 75 | raise ValueError("syntax error") # Division by zero |
| 76 | result //= divisor # Integer division as per examples |
| 77 | except ValueError: |
| 78 | raise ValueError("syntax error") |
| 79 | i += 3 |
| 80 | else: |
| 81 | # Edge Case: Handle unsupported operations |
| 82 | raise ValueError("unknown operation") |
| 83 | |
| 84 | # Edge Case: Check if we processed all tokens correctly |
| 85 | if i > len(tokens): |
| 86 | raise ValueError("syntax error") |
| 87 | |
| 88 | return result |
| 89 | # Handled Edge Cases: Non-math questions, empty expressions, single numbers, |
| 90 | # invalid numbers, missing operands, unsupported operations, |
| 91 | # division by zero, incomplete operations |