| 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 unsupported operations, |
| 13 | or is not a valid math question |
| 14 | """ |
| 15 | # Edge Case: Empty or None input |
| 16 | if not question: |
| 17 | raise ValueError("syntax error") |
| 18 | |
| 19 | # Edge Case: Non-math questions |
| 20 | if not question.startswith("What is"): |
| 21 | raise ValueError("unknown operation") |
| 22 | |
| 23 | # Edge Case: Malformed question without ending question mark |
| 24 | if not question.endswith("?"): |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Remove "What is" and "?" to get the expression |
| 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 | # Split the expression into tokens |
| 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 | # Handle two-word operations first |
| 55 | if operation == "multiplied": |
| 56 | if len(tokens) >= 3 and tokens[1] == "by": |
| 57 | try: |
| 58 | next_number = int(tokens[2]) |
| 59 | except ValueError: |
| 60 | raise ValueError("syntax error") |
| 61 | result *= next_number |
| 62 | tokens = tokens[3:] |
| 63 | continue |
| 64 | else: |
| 65 | raise ValueError("syntax error") |
| 66 | elif operation == "divided": |
| 67 | if len(tokens) >= 3 and tokens[1] == "by": |
| 68 | try: |
| 69 | next_number = int(tokens[2]) |
| 70 | except ValueError: |
| 71 | raise ValueError("syntax error") |
| 72 | if next_number == 0: |
| 73 | raise ValueError("syntax error") |
| 74 | result //= next_number |
| 75 | tokens = tokens[3:] |
| 76 | continue |
| 77 | else: |
| 78 | raise ValueError("syntax error") |
| 79 | |
| 80 | # Handle single-word operations |
| 81 | if operation not in ["plus", "minus"]: |
| 82 | # Unknown operation |
| 83 | raise ValueError("unknown operation") |
| 84 | |
| 85 | if len(tokens) < 2: |
| 86 | raise ValueError("syntax error") |
| 87 | |
| 88 | try: |
| 89 | next_number = int(tokens[1]) |
| 90 | except ValueError: |
| 91 | # If tokens[1] is not a number and we haven't handled it as a two-word operation, |
| 92 | # check if it's a known operation word (syntax error) or unknown operation |
| 93 | if tokens[1] in ["by", "plus", "minus", "multiplied", "divided"]: |
| 94 | raise ValueError("syntax error") |
| 95 | else: |
| 96 | raise ValueError("unknown operation") |
| 97 | |
| 98 | # Perform single-word operations |
| 99 | if operation == "plus": |
| 100 | result += next_number |
| 101 | elif operation == "minus": |
| 102 | result -= next_number |
| 103 | |
| 104 | # Move to next pair of tokens |
| 105 | tokens = tokens[2:] |
| 106 | |
| 107 | return result |
| 108 | # Handled Edge Cases: Empty or None input, Non-math questions, Malformed question without ending question mark, |
| 109 | # Empty expression, No tokens, First token is not a number, Odd number of tokens remaining, |
| 110 | # Not enough tokens for operation and operand, Second token is not a number, "multiplied" without "by", |
| 111 | # "divided" without "by", Division by zero, Unsupported operation |