| 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 unknown 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" 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 | # 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 | i += 1 |
| 51 | # Edge Case: Decimal numbers are not allowed |
| 52 | if '.' in expression[start:i]: |
| 53 | raise ValueError("syntax error") |
| 54 | tokens.append(int(expression[start:i])) |
| 55 | continue |
| 56 | |
| 57 | # Parse operations |
| 58 | if expression[i:].startswith("plus"): |
| 59 | tokens.append("+") |
| 60 | i += 4 |
| 61 | elif expression[i:].startswith("minus"): |
| 62 | tokens.append("-") |
| 63 | i += 5 |
| 64 | elif expression[i:].startswith("multiplied by"): |
| 65 | tokens.append("*") |
| 66 | i += 13 |
| 67 | elif expression[i:].startswith("divided by"): |
| 68 | tokens.append("/") |
| 69 | i += 10 |
| 70 | else: |
| 71 | # Edge Case: Unknown operations |
| 72 | raise ValueError("unknown operation") |
| 73 | |
| 74 | # Edge Case: Expression with no tokens |
| 75 | if not tokens: |
| 76 | raise ValueError("syntax error") |
| 77 | |
| 78 | # Edge Case: Expression starting with an operation |
| 79 | if isinstance(tokens[0], str): |
| 80 | raise ValueError("syntax error") |
| 81 | |
| 82 | # Evaluate the expression from left to right |
| 83 | result = tokens[0] |
| 84 | i = 1 |
| 85 | |
| 86 | while i < len(tokens): |
| 87 | # Edge Case: Consecutive operations or numbers |
| 88 | if i + 1 >= len(tokens): |
| 89 | raise ValueError("syntax error") |
| 90 | |
| 91 | # Edge Case: Expecting operation but found number |
| 92 | if not isinstance(tokens[i], str): |
| 93 | raise ValueError("syntax error") |
| 94 | |
| 95 | # Edge Case: Expecting number but found operation |
| 96 | if not isinstance(tokens[i + 1], int): |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | operation = tokens[i] |
| 100 | operand = tokens[i + 1] |
| 101 | |
| 102 | if operation == "+": |
| 103 | result += operand |
| 104 | elif operation == "-": |
| 105 | result -= operand |
| 106 | elif operation == "*": |
| 107 | result *= operand |
| 108 | elif operation == "/": |
| 109 | # Edge Case: Division by zero |
| 110 | if operand == 0: |
| 111 | raise ValueError("syntax error") |
| 112 | result //= operand # Integer division as per examples |
| 113 | else: |
| 114 | # Edge Case: Unknown operation (should not happen due to earlier validation) |
| 115 | raise ValueError("unknown operation") |
| 116 | |
| 117 | i += 2 |
| 118 | |
| 119 | return result |
| 120 | # Handled Edge Cases: Empty input, None input, non-math questions, questions without question mark, |
| 121 | # empty expressions, decimal numbers, unknown operations, expressions with no tokens, |
| 122 | # expressions starting with operations, consecutive operations or numbers, |
| 123 | # expecting operation but found number, expecting number but found operation, |
| 124 | # division by zero |