| 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 | An integer result of evaluating the math expression. |
| 10 | |
| 11 | Raises: |
| 12 | ValueError: If the question is malformed, contains unsupported operations, |
| 13 | or is not a math question. |
| 14 | """ |
| 15 | # Edge Case: Empty string 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 | # Remove "What is" and the trailing question mark |
| 24 | expression = question[8:-1].strip() |
| 25 | |
| 26 | # Edge Case: Empty expression after removing "What is" and "?" |
| 27 | if not expression: |
| 28 | raise ValueError("syntax error") |
| 29 | |
| 30 | # Split the expression into tokens |
| 31 | tokens = expression.split() |
| 32 | |
| 33 | # Edge Case: No tokens |
| 34 | if not tokens: |
| 35 | raise ValueError("syntax error") |
| 36 | |
| 37 | # Convert first token to integer |
| 38 | try: |
| 39 | result = int(tokens[0]) |
| 40 | except ValueError: |
| 41 | # Edge Case: First token is not a number |
| 42 | raise ValueError("syntax error") |
| 43 | |
| 44 | # Process the rest of the tokens |
| 45 | i = 1 |
| 46 | while i < len(tokens): |
| 47 | # Edge Case: Operation expected but we've reached the end |
| 48 | if i >= len(tokens): |
| 49 | raise ValueError("syntax error") |
| 50 | |
| 51 | operation = tokens[i] |
| 52 | |
| 53 | # Edge Case: Invalid operation |
| 54 | if operation not in ["plus", "minus", "multiplied", "divided"]: |
| 55 | raise ValueError("unknown operation") |
| 56 | |
| 57 | # Handle "multiplied by" and "divided by" |
| 58 | if operation in ["multiplied", "divided"]: |
| 59 | # Edge Case: Missing "by" after "multiplied" or "divided" |
| 60 | if i + 1 >= len(tokens) or tokens[i + 1] != "by": |
| 61 | raise ValueError("syntax error") |
| 62 | i += 1 # Skip "by" |
| 63 | |
| 64 | # Edge Case: Number expected but we've reached the end |
| 65 | if i + 1 >= len(tokens): |
| 66 | raise ValueError("syntax error") |
| 67 | |
| 68 | # Get the next number |
| 69 | try: |
| 70 | number = int(tokens[i + 1]) |
| 71 | except ValueError: |
| 72 | # Edge Case: Next token is not a number |
| 73 | raise ValueError("syntax error") |
| 74 | |
| 75 | # Perform the operation |
| 76 | if operation == "plus": |
| 77 | result += number |
| 78 | elif operation == "minus": |
| 79 | result -= number |
| 80 | elif operation == "multiplied": |
| 81 | result *= number |
| 82 | elif operation == "divided": |
| 83 | result //= number # Integer division as per examples |
| 84 | |
| 85 | i += 2 # Move to the next operation |
| 86 | |
| 87 | return result |
| 88 | # Handled Edge Cases: Empty input, non-math questions, missing question mark removal, |
| 89 | # empty expression, no tokens, first token not a number, |
| 90 | # operation expected but reached end, invalid operation, |
| 91 | # missing "by" after multiplied/divided, number expected but reached end, |
| 92 | # next token is not a number |