| 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 ending 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 | # 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 | # Edge Case: Unknown operations |
| 72 | else: |
| 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 not operand_str.lstrip('-').isdigit(): |
| 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 during evaluation |
| 113 | raise ValueError("unknown operation") |
| 114 | |
| 115 | i += 2 |
| 116 | |
| 117 | # Edge Case: Invalid syntax - trailing operator |
| 118 | if i > len(tokens) and i - 2 < len(tokens) - 1: |
| 119 | raise ValueError("syntax error") |
| 120 | |
| 121 | return result |
| 122 | except (ValueError, IndexError): |
| 123 | # Edge Case: General syntax errors during evaluation |
| 124 | raise ValueError("syntax error") |
| 125 | |
| 126 | # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark, |
| 127 | # empty expression, negative numbers, unknown operations, |
| 128 | # empty tokens, missing operators/operands, operator followed by operator, |
| 129 | # division by zero, trailing operators, general syntax errors |