| 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 | # Handle empty question |
| 15 | if not question: |
| 16 | raise ValueError("syntax error") |
| 17 | |
| 18 | # Edge Case: Non-math questions |
| 19 | if not question.startswith("What is"): |
| 20 | raise ValueError("syntax error") |
| 21 | |
| 22 | # Check for proper question format |
| 23 | if not question.endswith('?'): |
| 24 | raise ValueError("syntax error") |
| 25 | |
| 26 | # Remove the "What is" prefix and the trailing question mark |
| 27 | expression = question[8:-1].strip() |
| 28 | |
| 29 | # Edge Case: Empty expression after "What is" |
| 30 | if not expression: |
| 31 | raise ValueError("syntax error") |
| 32 | |
| 33 | # Split the expression into tokens |
| 34 | tokens = expression.split() |
| 35 | |
| 36 | # Edge Case: Expression with just a number |
| 37 | if len(tokens) == 1: |
| 38 | try: |
| 39 | return int(tokens[0]) |
| 40 | except ValueError: |
| 41 | # Edge Case: Single token that is not a number |
| 42 | raise ValueError("syntax error") |
| 43 | |
| 44 | # Parse the first number |
| 45 | try: |
| 46 | result = int(tokens[0]) |
| 47 | except ValueError: |
| 48 | # Edge Case: First token is not a number |
| 49 | raise ValueError("syntax error") |
| 50 | |
| 51 | i = 1 |
| 52 | |
| 53 | # Process operations from left to right |
| 54 | while i < len(tokens): |
| 55 | operation = tokens[i] |
| 56 | |
| 57 | # Handle two-word operations |
| 58 | if operation == "multiplied" or operation == "divided": |
| 59 | # Check if we have enough tokens for a two-word operation |
| 60 | if i + 2 >= len(tokens): |
| 61 | raise ValueError("syntax error") |
| 62 | |
| 63 | # Check if the second part is "by" |
| 64 | if tokens[i + 1] != "by": |
| 65 | raise ValueError("syntax error") |
| 66 | |
| 67 | # Get the operand |
| 68 | operand_str = tokens[i + 2] |
| 69 | try: |
| 70 | operand = int(operand_str) |
| 71 | except ValueError: |
| 72 | # Edge Case: Operand is not a number |
| 73 | raise ValueError("syntax error") |
| 74 | |
| 75 | # Perform the operation |
| 76 | if operation == "multiplied": |
| 77 | result *= operand |
| 78 | elif operation == "divided": |
| 79 | # Edge Case: Division by zero |
| 80 | if operand == 0: |
| 81 | raise ValueError("syntax error") |
| 82 | result = int(result / operand) |
| 83 | |
| 84 | # Move index past the entire operation (3 tokens: operation, by, operand) |
| 85 | i += 3 |
| 86 | else: |
| 87 | # Handle single-word operations |
| 88 | # First check if this is a known operation |
| 89 | if operation not in ["plus", "minus"]: |
| 90 | # Edge Case: Unknown operation |
| 91 | raise ValueError("unknown operation") |
| 92 | |
| 93 | # Edge Case: Missing operand |
| 94 | if i + 1 >= len(tokens): |
| 95 | raise ValueError("syntax error") |
| 96 | |
| 97 | operand_str = tokens[i + 1] |
| 98 | try: |
| 99 | operand = int(operand_str) |
| 100 | except ValueError: |
| 101 | # Edge Case: Operand is not a number |
| 102 | raise ValueError("syntax error") |
| 103 | |
| 104 | # Perform the operation |
| 105 | if operation == "plus": |
| 106 | result += operand |
| 107 | elif operation == "minus": |
| 108 | result -= operand |
| 109 | |
| 110 | # Move index past the operation and operand (2 tokens) |
| 111 | i += 2 |
| 112 | |
| 113 | return result |