| 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 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 (doesn't end with '?') |
| 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 | # Perform operation |
| 55 | if operation == "plus": |
| 56 | # Check if we have a number after the operation |
| 57 | if len(tokens) < 2: |
| 58 | raise ValueError("syntax error") |
| 59 | try: |
| 60 | next_number = int(tokens[1]) |
| 61 | except ValueError: |
| 62 | raise ValueError("syntax error") |
| 63 | result += next_number |
| 64 | tokens = tokens[2:] |
| 65 | elif operation == "minus": |
| 66 | # Check if we have a number after the operation |
| 67 | if len(tokens) < 2: |
| 68 | raise ValueError("syntax error") |
| 69 | try: |
| 70 | next_number = int(tokens[1]) |
| 71 | except ValueError: |
| 72 | raise ValueError("syntax error") |
| 73 | result -= next_number |
| 74 | tokens = tokens[2:] |
| 75 | elif operation == "multiplied": |
| 76 | # Check if we have "by" and a number after the operation |
| 77 | if len(tokens) < 3 or tokens[1] != "by": |
| 78 | raise ValueError("syntax error") |
| 79 | try: |
| 80 | next_number = int(tokens[2]) |
| 81 | except ValueError: |
| 82 | raise ValueError("syntax error") |
| 83 | result *= next_number |
| 84 | tokens = tokens[3:] |
| 85 | elif operation == "divided": |
| 86 | # Check if we have "by" and a number after the operation |
| 87 | if len(tokens) < 3 or tokens[1] != "by": |
| 88 | raise ValueError("syntax error") |
| 89 | try: |
| 90 | next_number = int(tokens[2]) |
| 91 | except ValueError: |
| 92 | raise ValueError("syntax error") |
| 93 | # Edge Case: Division by zero |
| 94 | if next_number == 0: |
| 95 | raise ValueError("syntax error") |
| 96 | result //= next_number |
| 97 | tokens = tokens[3:] |
| 98 | else: |
| 99 | # Edge Case: Unsupported operation |
| 100 | raise ValueError("unknown operation") |
| 101 | |
| 102 | return result |
| 103 | # Handled Edge Cases: Empty input, None input, non-math questions, malformed questions (no ending '?'), |
| 104 | # empty expression, no tokens, first token not a number, odd number of remaining tokens, |
| 105 | # missing operand after operation, next token not a number, division by zero, |
| 106 | # unsupported operations |