| 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: Non-math questions |
| 16 | if not question.startswith("What is"): |
| 17 | raise ValueError("unknown operation") |
| 18 | |
| 19 | # Remove the "What is" prefix and the trailing question mark |
| 20 | expression = question[8:-1].strip() |
| 21 | |
| 22 | # Edge Case: Empty expression after "What is" |
| 23 | if not expression: |
| 24 | raise ValueError("syntax error") |
| 25 | |
| 26 | # Tokenize the expression |
| 27 | tokens = [] |
| 28 | i = 0 |
| 29 | while i < len(expression): |
| 30 | if expression[i].isspace(): |
| 31 | i += 1 |
| 32 | continue |
| 33 | |
| 34 | # Parse numbers (including negative numbers) |
| 35 | if expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()): |
| 36 | start = i |
| 37 | if expression[i] == '-': |
| 38 | i += 1 |
| 39 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 40 | i += 1 |
| 41 | # Edge Case: Numbers with decimal points |
| 42 | num_str = expression[start:i] |
| 43 | if '.' in num_str: |
| 44 | raise ValueError("syntax error") |
| 45 | tokens.append(int(num_str)) |
| 46 | continue |
| 47 | |
| 48 | # Parse operations |
| 49 | if expression[i:].startswith("plus"): |
| 50 | tokens.append("+") |
| 51 | i += 4 |
| 52 | elif expression[i:].startswith("minus"): |
| 53 | tokens.append("-") |
| 54 | i += 5 |
| 55 | elif expression[i:].startswith("multiplied by"): |
| 56 | tokens.append("*") |
| 57 | i += 13 |
| 58 | elif expression[i:].startswith("divided by"): |
| 59 | tokens.append("/") |
| 60 | i += 10 |
| 61 | else: |
| 62 | # Edge Case: Unsupported operations |
| 63 | raise ValueError("unknown operation") |
| 64 | |
| 65 | # Edge Case: Expression with just a number |
| 66 | if len(tokens) == 1 and isinstance(tokens[0], int): |
| 67 | return tokens[0] |
| 68 | |
| 69 | # Edge Case: Invalid syntax - operations without numbers or vice versa |
| 70 | if len(tokens) < 3 or len(tokens) % 2 == 0: |
| 71 | raise ValueError("syntax error") |
| 72 | |
| 73 | # Evaluate the expression from left to right |
| 74 | result = tokens[0] |
| 75 | |
| 76 | # Edge Case: First token is not a number |
| 77 | if not isinstance(result, int): |
| 78 | raise ValueError("syntax error") |
| 79 | |
| 80 | i = 1 |
| 81 | while i < len(tokens): |
| 82 | # Edge Case: Missing operation or number in sequence |
| 83 | if i + 1 >= len(tokens): |
| 84 | raise ValueError("syntax error") |
| 85 | |
| 86 | operation = tokens[i] |
| 87 | operand = tokens[i + 1] |
| 88 | |
| 89 | # Edge Case: Invalid operation type |
| 90 | if not isinstance(operation, str): |
| 91 | raise ValueError("syntax error") |
| 92 | |
| 93 | # Edge Case: Operand is not a number |
| 94 | if not isinstance(operand, int): |
| 95 | raise ValueError("syntax error") |
| 96 | |
| 97 | if operation == "+": |
| 98 | result += operand |
| 99 | elif operation == "-": |
| 100 | result -= operand |
| 101 | elif operation == "*": |
| 102 | result *= operand |
| 103 | elif operation == "/": |
| 104 | # Edge Case: Division by zero |
| 105 | if operand == 0: |
| 106 | raise ValueError("syntax error") # Following test expectations |
| 107 | result //= operand # Integer division as per examples |
| 108 | else: |
| 109 | # Edge Case: Unknown operation (should not happen with current parsing) |
| 110 | raise ValueError("unknown operation") |
| 111 | |
| 112 | i += 2 |
| 113 | |
| 114 | return result |
| 115 | # Handled Edge Cases: Non-math questions, empty expression, numbers with decimal points, |
| 116 | # unsupported operations, expression with just a number, |
| 117 | # invalid syntax (operations without numbers or vice versa), |
| 118 | # first token not a number, missing operation or number in sequence, |
| 119 | # invalid operation type, operand not a number, division by zero, |
| 120 | # unknown operation during evaluation |