| 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 questions without a question mark |
| 24 | if not question.endswith("?"): |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Remove "What is " prefix and "?" suffix |
| 28 | expression = question[8:-1].strip() |
| 29 | |
| 30 | # Edge Case: Empty expression after removing prefix/suffix |
| 31 | if not expression: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | # Tokenize the expression |
| 35 | tokens = [] |
| 36 | i = 0 |
| 37 | |
| 38 | # Edge Case: Handle negative numbers at the start |
| 39 | if expression.startswith("-"): |
| 40 | i = 1 |
| 41 | while i < len(expression) and (expression[i].isdigit() or expression[i] == ' '): |
| 42 | i += 1 |
| 43 | tokens.append(expression[:i]) |
| 44 | |
| 45 | while i < len(expression): |
| 46 | if expression[i] == ' ': |
| 47 | i += 1 |
| 48 | continue |
| 49 | |
| 50 | # Parse numbers |
| 51 | if expression[i].isdigit() or (expression[i] == '-' and (i == 0 or expression[i-1] == ' ')): |
| 52 | start = i |
| 53 | if expression[i] == '-': |
| 54 | i += 1 |
| 55 | while i < len(expression) and expression[i].isdigit(): |
| 56 | i += 1 |
| 57 | tokens.append(expression[start:i]) |
| 58 | # Parse operators |
| 59 | elif expression[i:].startswith("plus"): |
| 60 | tokens.append("plus") |
| 61 | i += 4 |
| 62 | elif expression[i:].startswith("minus"): |
| 63 | tokens.append("minus") |
| 64 | i += 5 |
| 65 | elif expression[i:].startswith("multiplied by"): |
| 66 | tokens.append("multiplied by") |
| 67 | i += 13 |
| 68 | elif expression[i:].startswith("divided by"): |
| 69 | tokens.append("divided by") |
| 70 | i += 10 |
| 71 | else: |
| 72 | # Edge Case: Unknown operations |
| 73 | raise ValueError("unknown operation") |
| 74 | |
| 75 | # Edge Case: Empty tokens list |
| 76 | if not tokens: |
| 77 | raise ValueError("syntax error") |
| 78 | |
| 79 | # Evaluate the expression from left to right |
| 80 | try: |
| 81 | # First token should be a number |
| 82 | result = int(tokens[0]) |
| 83 | i = 1 |
| 84 | |
| 85 | # Process operations in pairs (operator, number) |
| 86 | while i < len(tokens): |
| 87 | # Edge Case: Missing operator or operand |
| 88 | if i + 1 >= len(tokens): |
| 89 | raise ValueError("syntax error") |
| 90 | |
| 91 | operator = tokens[i] |
| 92 | operand_str = tokens[i + 1] |
| 93 | |
| 94 | # Edge Case: Operator followed by another operator |
| 95 | if operand_str in ["plus", "minus", "multiplied by", "divided by"]: |
| 96 | raise ValueError("syntax error") |
| 97 | |
| 98 | operand = int(operand_str) |
| 99 | |
| 100 | if operator == "plus": |
| 101 | result += operand |
| 102 | elif operator == "minus": |
| 103 | result -= operand |
| 104 | elif operator == "multiplied by": |
| 105 | result *= operand |
| 106 | elif operator == "divided by": |
| 107 | # Edge Case: Division by zero |
| 108 | if operand == 0: |
| 109 | raise ValueError("syntax error") |
| 110 | result //= operand # Integer division as per examples |
| 111 | else: |
| 112 | # Edge Case: Unknown operation (should not happen with current parsing) |
| 113 | raise ValueError("unknown operation") |
| 114 | |
| 115 | i += 2 |
| 116 | |
| 117 | # Edge Case: Consecutive operators |
| 118 | if len(tokens) > 1 and i <= len(tokens): |
| 119 | if tokens[-1] in ["plus", "minus", "multiplied by", "divided by"]: |
| 120 | raise ValueError("syntax error") |
| 121 | |
| 122 | return result |
| 123 | |
| 124 | except ValueError as e: |
| 125 | # Re-raise ValueError with appropriate message if it's already a syntax or unknown operation error |
| 126 | if "syntax error" in str(e) or "unknown operation" in str(e): |
| 127 | raise |
| 128 | # Edge Case: Invalid number format |
| 129 | raise ValueError("syntax error") |
| 130 | |
| 131 | # Edge Case: Any other unexpected error during evaluation |
| 132 | except Exception: |
| 133 | raise ValueError("syntax error") |
| 134 | |
| 135 | # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark, |
| 136 | # empty expression, unknown operations, negative numbers, |
| 137 | # missing operators/operands, consecutive operators, |
| 138 | # division by zero, invalid number format, |
| 139 | # unexpected errors during evaluation |