| 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 question (doesn't end with ?) |
| 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 | while i < len(expression): |
| 38 | if expression[i].isspace(): |
| 39 | i += 1 |
| 40 | continue |
| 41 | elif expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()): |
| 42 | # Parse number (including negative numbers) |
| 43 | start = i |
| 44 | if expression[i] == '-': |
| 45 | i += 1 |
| 46 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 47 | i += 1 |
| 48 | # Edge Case: Invalid number format (decimals not allowed per problem description) |
| 49 | num_str = expression[start:i] |
| 50 | if '.' in num_str: |
| 51 | raise ValueError("syntax error") |
| 52 | try: |
| 53 | tokens.append(int(num_str)) |
| 54 | except ValueError: |
| 55 | raise ValueError("syntax error") |
| 56 | else: |
| 57 | # Parse word operators |
| 58 | if expression[i:].startswith("plus"): |
| 59 | tokens.append("plus") |
| 60 | i += 4 |
| 61 | elif expression[i:].startswith("minus"): |
| 62 | tokens.append("minus") |
| 63 | i += 5 |
| 64 | elif expression[i:].startswith("multiplied by"): |
| 65 | tokens.append("multiplied by") |
| 66 | i += 13 |
| 67 | elif expression[i:].startswith("divided by"): |
| 68 | tokens.append("divided by") |
| 69 | i += 10 |
| 70 | else: |
| 71 | # Edge Case: Unknown operations |
| 72 | raise ValueError("unknown operation") |
| 73 | |
| 74 | # Edge Case: Empty tokens list |
| 75 | if not tokens: |
| 76 | raise ValueError("syntax error") |
| 77 | |
| 78 | # Edge Case: Expression starting with an operator |
| 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 | # Edge Case: Odd number of tokens (incomplete expression) |
| 87 | if len(tokens) % 2 == 0: |
| 88 | raise ValueError("syntax error") |
| 89 | |
| 90 | while i < len(tokens): |
| 91 | # Edge Case: Expected operator but found number |
| 92 | if not isinstance(tokens[i], str): |
| 93 | raise ValueError("syntax error") |
| 94 | |
| 95 | # Edge Case: Expected number but found operator |
| 96 | if i + 1 >= len(tokens) or isinstance(tokens[i + 1], str): |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | operator = tokens[i] |
| 100 | operand = tokens[i + 1] |
| 101 | |
| 102 | if operator == "plus": |
| 103 | result += operand |
| 104 | elif operator == "minus": |
| 105 | result -= operand |
| 106 | elif operator == "multiplied by": |
| 107 | result *= operand |
| 108 | elif operator == "divided by": |
| 109 | # Edge Case: Division by zero |
| 110 | if operand == 0: |
| 111 | raise ValueError("syntax error") # Following test expectations |
| 112 | result //= operand # Integer division as per examples |
| 113 | else: |
| 114 | # Edge Case: Unsupported operations should have been caught earlier |
| 115 | raise ValueError("unknown operation") |
| 116 | |
| 117 | i += 2 |
| 118 | |
| 119 | return result |
| 120 | # Handled Edge Cases: Empty/None input, non-math questions, malformed questions, |
| 121 | # empty expressions, invalid number formats, unknown operations, |
| 122 | # expressions starting with operators, odd number of tokens, |
| 123 | # incorrect token sequences, division by zero |