| 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 questions without a question mark |
| 24 | if not question.endswith("?"): |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Remove "What is" prefix and question mark |
| 28 | expression = question[8:-1].strip() |
| 29 | |
| 30 | # Edge Case: Empty expression after removing prefix and suffix |
| 31 | if not expression: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | # Tokenize the expression |
| 35 | tokens = [] |
| 36 | i = 0 |
| 37 | |
| 38 | while i < len(expression): |
| 39 | # Skip whitespace |
| 40 | if expression[i].isspace(): |
| 41 | i += 1 |
| 42 | continue |
| 43 | |
| 44 | # Parse numbers (including negative numbers) |
| 45 | if expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()): |
| 46 | start = i |
| 47 | if expression[i] == '-': |
| 48 | i += 1 |
| 49 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '-'): |
| 50 | if expression[i] == '-': |
| 51 | # Only allow '-' at the beginning of a number |
| 52 | if i != start: |
| 53 | raise ValueError("syntax error") |
| 54 | i += 1 |
| 55 | try: |
| 56 | tokens.append(int(expression[start:i])) |
| 57 | except ValueError: |
| 58 | raise ValueError("syntax error") |
| 59 | continue |
| 60 | |
| 61 | # Parse operators |
| 62 | if expression[i:].startswith("plus"): |
| 63 | tokens.append("+") |
| 64 | i += 4 |
| 65 | elif expression[i:].startswith("minus"): |
| 66 | tokens.append("-") |
| 67 | i += 5 |
| 68 | elif expression[i:].startswith("multiplied by"): |
| 69 | tokens.append("*") |
| 70 | i += 13 |
| 71 | elif expression[i:].startswith("divided by"): |
| 72 | tokens.append("/") |
| 73 | i += 10 |
| 74 | else: |
| 75 | # Edge Case: Unsupported operations |
| 76 | raise ValueError("unknown operation") |
| 77 | |
| 78 | # Edge Case: Empty tokens list |
| 79 | if not tokens: |
| 80 | raise ValueError("syntax error") |
| 81 | |
| 82 | # Edge Case: Expression starting with an operator |
| 83 | if isinstance(tokens[0], str): |
| 84 | raise ValueError("syntax error") |
| 85 | |
| 86 | # Evaluate the expression from left to right |
| 87 | result = tokens[0] |
| 88 | i = 1 |
| 89 | |
| 90 | # Edge Case: Odd number of tokens (missing operator or operand) |
| 91 | if len(tokens) % 2 == 0: |
| 92 | raise ValueError("syntax error") |
| 93 | |
| 94 | while i < len(tokens): |
| 95 | # Edge Case: Expecting operator but found number |
| 96 | if not isinstance(tokens[i], str): |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | operator = tokens[i] |
| 100 | |
| 101 | # Edge Case: Expecting number but found operator |
| 102 | if i + 1 >= len(tokens) or isinstance(tokens[i + 1], str): |
| 103 | raise ValueError("syntax error") |
| 104 | |
| 105 | operand = tokens[i + 1] |
| 106 | |
| 107 | if operator == "+": |
| 108 | result += operand |
| 109 | elif operator == "-": |
| 110 | result -= operand |
| 111 | elif operator == "*": |
| 112 | result *= operand |
| 113 | elif operator == "/": |
| 114 | # Edge Case: Division by zero |
| 115 | if operand == 0: |
| 116 | raise ValueError("syntax error") |
| 117 | result //= operand # Integer division as per examples |
| 118 | else: |
| 119 | # Edge Case: Unknown operator (should not happen with current parsing) |
| 120 | raise ValueError("unknown operation") |
| 121 | |
| 122 | i += 2 |
| 123 | |
| 124 | return result |
| 125 | # Handled Edge Cases: Empty or None input, Non-math questions, Malformed questions without a question mark, |
| 126 | # Empty expression, Unsupported operations, Empty tokens list, Expression starting with an operator, |
| 127 | # Odd number of tokens, Expecting operator but found number, Expecting number but found operator, |
| 128 | # Division by zero, Unknown operator |