| 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 evaluating the math expression |
| 10 | |
| 11 | Raises: |
| 12 | ValueError: If the question is malformed, contains unknown operations, or is not a math question |
| 13 | """ |
| 14 | # Edge Case: Empty or None input |
| 15 | if not question: |
| 16 | raise ValueError("syntax error") |
| 17 | |
| 18 | # Edge Case: Question doesn't start with "What is" |
| 19 | if not question.startswith("What is"): |
| 20 | raise ValueError("unknown operation") |
| 21 | |
| 22 | # Remove "What is" and "?" from the question |
| 23 | expression = question[8:-1].strip() |
| 24 | |
| 25 | # Edge Case: Empty expression after removing "What is" and "?" |
| 26 | if not expression: |
| 27 | raise ValueError("syntax error") |
| 28 | |
| 29 | # Split the expression into tokens |
| 30 | tokens = expression.split() |
| 31 | |
| 32 | # Edge Case: No tokens |
| 33 | if not tokens: |
| 34 | raise ValueError("syntax error") |
| 35 | |
| 36 | # Parse the first number |
| 37 | try: |
| 38 | result = int(tokens[0]) |
| 39 | tokens = tokens[1:] |
| 40 | except ValueError: |
| 41 | # Edge Case: First token is not a number |
| 42 | raise ValueError("syntax error") |
| 43 | |
| 44 | # Process operations |
| 45 | i = 0 |
| 46 | while i < len(tokens): |
| 47 | # Edge Case: Unexpected end of tokens (missing operand) |
| 48 | if i >= len(tokens): |
| 49 | raise ValueError("syntax error") |
| 50 | |
| 51 | op = tokens[i] |
| 52 | i += 1 |
| 53 | |
| 54 | # Edge Case: Missing operand after operation |
| 55 | if i >= len(tokens): |
| 56 | raise ValueError("syntax error") |
| 57 | |
| 58 | # Handle different operations |
| 59 | if op == "plus": |
| 60 | try: |
| 61 | operand = int(tokens[i]) |
| 62 | result += operand |
| 63 | except ValueError: |
| 64 | # Edge Case: Operand is not a number |
| 65 | raise ValueError("syntax error") |
| 66 | elif op == "minus": |
| 67 | try: |
| 68 | operand = int(tokens[i]) |
| 69 | result -= operand |
| 70 | except ValueError: |
| 71 | # Edge Case: Operand is not a number |
| 72 | raise ValueError("syntax error") |
| 73 | elif op == "multiplied" and i + 1 < len(tokens) and tokens[i] == "by": |
| 74 | # Handle "multiplied by" as a single operation |
| 75 | i += 1 |
| 76 | if i >= len(tokens): |
| 77 | raise ValueError("syntax error") |
| 78 | try: |
| 79 | operand = int(tokens[i]) |
| 80 | result *= operand |
| 81 | except ValueError: |
| 82 | # Edge Case: Operand is not a number |
| 83 | raise ValueError("syntax error") |
| 84 | elif op == "divided" and i + 1 < len(tokens) and tokens[i] == "by": |
| 85 | # Handle "divided by" as a single operation |
| 86 | i += 1 |
| 87 | if i >= len(tokens): |
| 88 | raise ValueError("syntax error") |
| 89 | try: |
| 90 | operand = int(tokens[i]) |
| 91 | if operand == 0: |
| 92 | # Edge Case: Division by zero |
| 93 | raise ValueError("syntax error") |
| 94 | result //= operand # Integer division as per examples |
| 95 | except ValueError: |
| 96 | # Edge Case: Operand is not a number |
| 97 | raise ValueError("syntax error") |
| 98 | else: |
| 99 | # Edge Case: Unknown operation |
| 100 | raise ValueError("unknown operation") |
| 101 | |
| 102 | i += 1 |
| 103 | |
| 104 | return result |
| 105 | # Handled Edge Cases: Empty or None input, Question doesn't start with "What is", Empty expression, No tokens, First token is not a number, Unexpected end of tokens, Missing operand after operation, Operand is not a number, Division by zero, Unknown operation |