| 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 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 | while i < len(expression): |
| 38 | if expression[i].isspace(): |
| 39 | i += 1 |
| 40 | continue |
| 41 | |
| 42 | # Handle negative numbers |
| 43 | if expression[i] == '-' and (i == 0 or expression[i-1] in ' '): |
| 44 | start = i |
| 45 | i += 1 |
| 46 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 47 | i += 1 |
| 48 | tokens.append(expression[start:i]) |
| 49 | # Handle positive numbers |
| 50 | elif expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()): |
| 51 | start = i |
| 52 | if expression[i] == '-': |
| 53 | i += 1 |
| 54 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 55 | i += 1 |
| 56 | tokens.append(expression[start:i]) |
| 57 | # Handle operators |
| 58 | elif 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: Expression with no tokens |
| 75 | if not tokens: |
| 76 | raise ValueError("syntax error") |
| 77 | |
| 78 | # Edge Case: Expression starting with an operator |
| 79 | if tokens[0] in ['plus', 'minus', 'multiplied by', 'divided by']: |
| 80 | raise ValueError("syntax error") |
| 81 | |
| 82 | # Convert first token to number |
| 83 | try: |
| 84 | result = int(tokens[0]) |
| 85 | except ValueError: |
| 86 | # Edge Case: First token is not a valid number |
| 87 | raise ValueError("syntax error") |
| 88 | |
| 89 | i = 1 |
| 90 | while i < len(tokens): |
| 91 | # Edge Case: Incomplete expression (operator with no following number) |
| 92 | if i + 1 >= len(tokens): |
| 93 | raise ValueError("syntax error") |
| 94 | |
| 95 | operator = tokens[i] |
| 96 | try: |
| 97 | operand = int(tokens[i + 1]) |
| 98 | except ValueError: |
| 99 | # Edge Case: Operand is not a valid number |
| 100 | raise ValueError("syntax error") |
| 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") |
| 112 | result //= operand # Integer division as per examples |
| 113 | else: |
| 114 | # Edge Case: Unknown operator (should not happen with current logic, but for safety) |
| 115 | raise ValueError("unknown operation") |
| 116 | |
| 117 | i += 2 |
| 118 | |
| 119 | # Edge Case: Expression ending with an operator |
| 120 | if len(tokens) > 1 and tokens[-1] in ['plus', 'minus', 'multiplied by', 'divided by']: |
| 121 | raise ValueError("syntax error") |
| 122 | |
| 123 | return result |
| 124 | # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark, |
| 125 | # empty expression, unknown operations, expression starting with operator, |
| 126 | # first token not a number, incomplete expression, operand not a number, |
| 127 | # division by zero, expression ending with operator |