| 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 (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 "?") |
| 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: Expression with just a number |
| 38 | if len(tokens) == 1: |
| 39 | try: |
| 40 | return int(tokens[0]) |
| 41 | except ValueError: |
| 42 | # Edge Case: Single token that is not a number |
| 43 | raise ValueError("syntax error") |
| 44 | |
| 45 | # Parse the tokens |
| 46 | equation = [] |
| 47 | i = 0 |
| 48 | |
| 49 | while i < len(tokens): |
| 50 | token = tokens[i] |
| 51 | |
| 52 | # Try to parse as number |
| 53 | try: |
| 54 | num = int(token) |
| 55 | equation.append(num) |
| 56 | except ValueError: |
| 57 | # Handle operations |
| 58 | if token == "plus": |
| 59 | equation.append("+") |
| 60 | elif token == "minus": |
| 61 | equation.append("-") |
| 62 | elif token == "multiplied" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 63 | # Edge Case: "multiplied by" as a two-word operation |
| 64 | equation.append("*") |
| 65 | i += 1 # Skip "by" |
| 66 | elif token == "divided" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 67 | # Edge Case: "divided by" as a two-word operation |
| 68 | equation.append("/") |
| 69 | i += 1 # Skip "by" |
| 70 | else: |
| 71 | # Edge Case: Unsupported operations |
| 72 | raise ValueError("unknown operation") |
| 73 | |
| 74 | i += 1 |
| 75 | |
| 76 | # Edge Case: Invalid syntax (even number of elements, should be odd: number-operation-number...) |
| 77 | if len(equation) % 2 == 0: |
| 78 | raise ValueError("syntax error") |
| 79 | |
| 80 | # Edge Case: Invalid syntax (first element should be a number) |
| 81 | if not isinstance(equation[0], int): |
| 82 | raise ValueError("syntax error") |
| 83 | |
| 84 | # Evaluate the expression from left to right |
| 85 | result = equation[0] |
| 86 | |
| 87 | # Process operations in pairs (operation, number) |
| 88 | i = 1 |
| 89 | while i < len(equation): |
| 90 | # Edge Case: Missing operation or number |
| 91 | if i + 1 >= len(equation): |
| 92 | raise ValueError("syntax error") |
| 93 | |
| 94 | operation = equation[i] |
| 95 | operand = equation[i + 1] |
| 96 | |
| 97 | # Edge Case: Invalid operation type |
| 98 | if not isinstance(operation, str): |
| 99 | raise ValueError("syntax error") |
| 100 | |
| 101 | # Edge Case: Invalid operand type |
| 102 | if not isinstance(operand, int): |
| 103 | raise ValueError("syntax error") |
| 104 | |
| 105 | if operation == "+": |
| 106 | result += operand |
| 107 | elif operation == "-": |
| 108 | result -= operand |
| 109 | elif operation == "*": |
| 110 | result *= operand |
| 111 | elif operation == "/": |
| 112 | # Edge Case: Division by zero |
| 113 | if operand == 0: |
| 114 | raise ValueError("syntax error") |
| 115 | result //= operand # Integer division as specified |
| 116 | else: |
| 117 | # Edge Case: Unknown operation |
| 118 | raise ValueError("unknown operation") |
| 119 | |
| 120 | i += 2 |
| 121 | |
| 122 | return result |
| 123 | # Handled Edge Cases: Empty input, None input, non-math questions, malformed questions (no question mark), |
| 124 | # empty expression, single non-number token, unsupported operations, |
| 125 | # "multiplied by" and "divided by" as two-word operations, |
| 126 | # invalid syntax (even number of elements), first element not a number, |
| 127 | # missing operation or number, invalid operation type, invalid operand type, |
| 128 | # division by zero, unknown operation during evaluation |