| 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 math operation |
| 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: Malformed question (doesn't end with '?' or is too short) |
| 24 | if not question.endswith('?'): |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Remove "What is" and the trailing question mark |
| 28 | expression = question[8:-1].strip() |
| 29 | |
| 30 | # Edge Case: Empty expression after removing "What is" and '?' |
| 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 | tokens = tokens[1:] |
| 45 | except ValueError: |
| 46 | # Edge Case: First token is not a number |
| 47 | raise ValueError("syntax error") |
| 48 | |
| 49 | # Process operations |
| 50 | while tokens: |
| 51 | # Get operation |
| 52 | operation = tokens[0] |
| 53 | |
| 54 | # Edge Case: Invalid operation |
| 55 | if operation not in ["plus", "minus", "multiplied", "divided"]: |
| 56 | raise ValueError("unknown operation") |
| 57 | |
| 58 | # Edge Case: Incomplete operation (missing number after operation) |
| 59 | if len(tokens) < 2: |
| 60 | raise ValueError("syntax error") |
| 61 | |
| 62 | # Handle "multiplied by" and "divided by" |
| 63 | if operation in ["multiplied", "divided"]: |
| 64 | # Edge Case: Missing "by" after "multiplied" or "divided" |
| 65 | if len(tokens) < 2 or tokens[1] != "by": |
| 66 | raise ValueError("syntax error") |
| 67 | tokens = tokens[1:] # Remove "by" |
| 68 | |
| 69 | # Edge Case: No number after operation |
| 70 | if len(tokens) < 2: |
| 71 | raise ValueError("syntax error") |
| 72 | |
| 73 | # Get the next number |
| 74 | try: |
| 75 | number = int(tokens[1]) |
| 76 | except ValueError: |
| 77 | # Edge Case: Token after operation is not a number |
| 78 | raise ValueError("syntax error") |
| 79 | |
| 80 | # Perform operation |
| 81 | if operation == "plus": |
| 82 | result += number |
| 83 | elif operation == "minus": |
| 84 | result -= number |
| 85 | elif operation == "multiplied": |
| 86 | result *= number |
| 87 | elif operation == "divided": |
| 88 | # Edge Case: Division by zero |
| 89 | if number == 0: |
| 90 | raise ValueError("syntax error") |
| 91 | result //= number # Integer division as per examples |
| 92 | |
| 93 | # Move to next operation |
| 94 | tokens = tokens[2:] |
| 95 | |
| 96 | return result |
| 97 | # Handled Edge Cases: Empty input, None input, questions not starting with "What is", |
| 98 | # questions not ending with '?', empty expressions, no tokens, |
| 99 | # first token not a number, odd number of remaining tokens, |
| 100 | # invalid operations, missing "by" after "multiplied" or "divided", |
| 101 | # no number after operation, token after operation not a number, |
| 102 | # division by zero |