| 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 '-' as part of a negative number at the beginning |
| 52 | if i != start + 1 or expression[start] != '-': |
| 53 | break |
| 54 | i += 1 |
| 55 | try: |
| 56 | num = int(expression[start:i]) |
| 57 | tokens.append(num) |
| 58 | except ValueError: |
| 59 | raise ValueError("syntax error") |
| 60 | continue |
| 61 | |
| 62 | # Parse operators |
| 63 | if expression[i:].startswith("plus"): |
| 64 | tokens.append("+") |
| 65 | i += 4 |
| 66 | elif expression[i:].startswith("minus"): |
| 67 | tokens.append("-") |
| 68 | i += 5 |
| 69 | elif expression[i:].startswith("multiplied by"): |
| 70 | tokens.append("*") |
| 71 | i += 13 |
| 72 | elif expression[i:].startswith("divided by"): |
| 73 | tokens.append("/") |
| 74 | i += 10 |
| 75 | else: |
| 76 | # Edge Case: Unknown operations |
| 77 | raise ValueError("unknown operation") |
| 78 | |
| 79 | # Edge Case: Expression with no tokens |
| 80 | if not tokens: |
| 81 | raise ValueError("syntax error") |
| 82 | |
| 83 | # Edge Case: Expression starting with an operator (except negative number) |
| 84 | if isinstance(tokens[0], str): |
| 85 | raise ValueError("syntax error") |
| 86 | |
| 87 | # Evaluate the expression from left to right |
| 88 | # Edge Case: Single number |
| 89 | if len(tokens) == 1: |
| 90 | if isinstance(tokens[0], int): |
| 91 | return tokens[0] |
| 92 | else: |
| 93 | raise ValueError("syntax error") |
| 94 | |
| 95 | # Edge Case: Invalid syntax - operators without numbers or vice versa |
| 96 | if len(tokens) % 2 == 0: |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | result = tokens[0] |
| 100 | |
| 101 | # Process operations sequentially |
| 102 | i = 1 |
| 103 | while i < len(tokens): |
| 104 | # Edge Case: Missing operator |
| 105 | if not isinstance(tokens[i], str): |
| 106 | raise ValueError("syntax error") |
| 107 | |
| 108 | # Edge Case: Missing operand |
| 109 | if i + 1 >= len(tokens) or not isinstance(tokens[i + 1], int): |
| 110 | raise ValueError("syntax error") |
| 111 | |
| 112 | operator = tokens[i] |
| 113 | operand = tokens[i + 1] |
| 114 | |
| 115 | if operator == "+": |
| 116 | result += operand |
| 117 | elif operator == "-": |
| 118 | result -= operand |
| 119 | elif operator == "*": |
| 120 | result *= operand |
| 121 | elif operator == "/": |
| 122 | # Edge Case: Division by zero |
| 123 | if operand == 0: |
| 124 | raise ValueError("syntax error") |
| 125 | result //= operand # Integer division as per examples |
| 126 | else: |
| 127 | # Edge Case: Unknown operator (should not happen with our parsing) |
| 128 | raise ValueError("unknown operation") |
| 129 | |
| 130 | i += 2 |
| 131 | |
| 132 | return result |
| 133 | # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark, |
| 134 | # empty expression, unknown operations, invalid syntax, |
| 135 | # single number, missing operators/operands, division by zero, |
| 136 | # expressions with wrong parity of tokens, negative numbers |