| 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, |
| 13 | or is not a math question |
| 14 | """ |
| 15 | # Edge Case: Empty or None input |
| 16 | if not question: |
| 17 | raise ValueError("syntax error") |
| 18 | |
| 19 | # Edge Case: Question doesn't start with "What is" |
| 20 | if not question.startswith("What is"): |
| 21 | raise ValueError("unknown operation") |
| 22 | |
| 23 | # Edge Case: Question is just "What is" with no numbers or operations |
| 24 | if question == "What is": |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Remove the "What is" prefix and the question mark |
| 28 | expression = question[8:].rstrip('?').strip() |
| 29 | |
| 30 | # Edge Case: Empty expression after removing prefix and suffix |
| 31 | if not expression: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | # Tokenize the expression |
| 35 | tokens = expression.split() |
| 36 | |
| 37 | # Edge Case: No tokens |
| 38 | if not tokens: |
| 39 | raise ValueError("syntax error") |
| 40 | |
| 41 | # Parse the first number |
| 42 | try: |
| 43 | result = int(tokens[0]) |
| 44 | i = 1 |
| 45 | except ValueError: |
| 46 | # Edge Case: First token is not a number |
| 47 | raise ValueError("syntax error") |
| 48 | |
| 49 | # Process operations |
| 50 | while i < len(tokens): |
| 51 | # Edge Case: Unexpected end of expression |
| 52 | if i >= len(tokens): |
| 53 | raise ValueError("syntax error") |
| 54 | |
| 55 | operation = tokens[i] |
| 56 | |
| 57 | # Perform the operation |
| 58 | if operation == "plus": |
| 59 | # Need at least one more token for the operand |
| 60 | if i + 1 >= len(tokens): |
| 61 | raise ValueError("syntax error") |
| 62 | try: |
| 63 | operand = int(tokens[i + 1]) |
| 64 | except ValueError: |
| 65 | raise ValueError("syntax error") |
| 66 | result += operand |
| 67 | i += 2 |
| 68 | elif operation == "minus": |
| 69 | # Need at least one more token for the operand |
| 70 | if i + 1 >= len(tokens): |
| 71 | raise ValueError("syntax error") |
| 72 | try: |
| 73 | operand = int(tokens[i + 1]) |
| 74 | except ValueError: |
| 75 | raise ValueError("syntax error") |
| 76 | result -= operand |
| 77 | i += 2 |
| 78 | elif operation == "multiplied": |
| 79 | # Handle "multiplied by" as a two-word operation |
| 80 | if i + 2 >= len(tokens) or tokens[i + 1] != "by": |
| 81 | raise ValueError("syntax error") |
| 82 | try: |
| 83 | operand = int(tokens[i + 2]) |
| 84 | except ValueError: |
| 85 | raise ValueError("syntax error") |
| 86 | result *= operand |
| 87 | i += 3 # Skip "multiplied", "by", and the operand |
| 88 | elif operation == "divided": |
| 89 | # Handle "divided by" as a two-word operation |
| 90 | if i + 2 >= len(tokens) or tokens[i + 1] != "by": |
| 91 | raise ValueError("syntax error") |
| 92 | try: |
| 93 | operand = int(tokens[i + 2]) |
| 94 | except ValueError: |
| 95 | raise ValueError("syntax error") |
| 96 | # Edge Case: Division by zero |
| 97 | if operand == 0: |
| 98 | raise ZeroDivisionError("division by zero") |
| 99 | result //= operand |
| 100 | i += 3 # Skip "divided", "by", and the operand |
| 101 | elif operation == "by": |
| 102 | # Edge Case: "by" without a preceding "multiplied" or "divided" |
| 103 | raise ValueError("syntax error") |
| 104 | else: |
| 105 | # Edge Case: Unknown operation |
| 106 | raise ValueError("unknown operation") |
| 107 | |
| 108 | return result |
| 109 | # Handled Edge Cases: Empty input, non-math questions, missing numbers, |
| 110 | # unknown operations, syntax errors, division by zero, |
| 111 | # malformed expressions, unexpected end of expression |