| 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: Question doesn't start with "What is" |
| 20 | if not question.startswith("What is"): |
| 21 | raise ValueError("unknown operation") |
| 22 | |
| 23 | # Edge Case: Malformed question (doesn't end with '?' or is too short) |
| 24 | if not question.endswith('?'): |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Extract the expression part |
| 28 | expression = question[8:-1].strip() # Remove "What is " and "?" |
| 29 | |
| 30 | # Edge Case: Empty expression |
| 31 | if not expression: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | # Tokenize the expression |
| 35 | tokens = [] |
| 36 | i = 0 |
| 37 | |
| 38 | # Edge Case: Invalid syntax - expression starts with an operator |
| 39 | while i < len(expression): |
| 40 | if expression[i].isspace(): |
| 41 | i += 1 |
| 42 | continue |
| 43 | |
| 44 | # Handle negative numbers |
| 45 | if expression[i] == '-' and (i == 0 or expression[i-1] in ' '): |
| 46 | i += 1 |
| 47 | if i >= len(expression) or not expression[i].isdigit(): |
| 48 | # This is a minus operator, not a negative sign |
| 49 | tokens.append('-') |
| 50 | continue |
| 51 | # This is a negative number |
| 52 | start = i - 1 |
| 53 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 54 | i += 1 |
| 55 | tokens.append(int(expression[start:i])) |
| 56 | continue |
| 57 | |
| 58 | # Handle positive numbers |
| 59 | if expression[i].isdigit(): |
| 60 | start = i |
| 61 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 62 | i += 1 |
| 63 | tokens.append(int(expression[start:i])) |
| 64 | continue |
| 65 | |
| 66 | # Handle words (operations) |
| 67 | if expression[i].isalpha(): |
| 68 | start = i |
| 69 | while i < len(expression) and expression[i].isalpha(): |
| 70 | i += 1 |
| 71 | word = expression[start:i] |
| 72 | if word == 'plus': |
| 73 | tokens.append('+') |
| 74 | elif word == 'minus': |
| 75 | tokens.append('-') |
| 76 | elif word == 'multiplied': |
| 77 | # Check for "by" |
| 78 | if i >= len(expression) or not expression[i:].lstrip().startswith('by'): |
| 79 | raise ValueError("syntax error") |
| 80 | # Skip "by" |
| 81 | while i < len(expression) and expression[i] != 'b': |
| 82 | i += 1 |
| 83 | i += 2 # Skip 'b' and 'y' |
| 84 | tokens.append('*') |
| 85 | elif word == 'divided': |
| 86 | # Check for "by" |
| 87 | if i >= len(expression) or not expression[i:].lstrip().startswith('by'): |
| 88 | raise ValueError("syntax error") |
| 89 | # Skip "by" |
| 90 | while i < len(expression) and expression[i] != 'b': |
| 91 | i += 1 |
| 92 | i += 2 # Skip 'b' and 'y' |
| 93 | tokens.append('/') |
| 94 | else: |
| 95 | raise ValueError("unknown operation") |
| 96 | continue |
| 97 | |
| 98 | # Handle operators |
| 99 | if expression[i] in '+-*/': |
| 100 | tokens.append(expression[i]) |
| 101 | i += 1 |
| 102 | continue |
| 103 | |
| 104 | # Edge Case: Invalid character |
| 105 | raise ValueError("syntax error") |
| 106 | |
| 107 | # Edge Case: No tokens |
| 108 | if not tokens: |
| 109 | raise ValueError("syntax error") |
| 110 | |
| 111 | # Edge Case: Expression starts with an operator |
| 112 | if isinstance(tokens[0], str) and tokens[0] in '+-*/': |
| 113 | raise ValueError("syntax error") |
| 114 | |
| 115 | # Edge Case: Expression ends with an operator |
| 116 | if isinstance(tokens[-1], str) and tokens[-1] in '+-*/': |
| 117 | raise ValueError("syntax error") |
| 118 | |
| 119 | # Evaluate the expression from left to right |
| 120 | # Edge Case: Single number |
| 121 | if len(tokens) == 1: |
| 122 | if isinstance(tokens[0], int): |
| 123 | return tokens[0] |
| 124 | else: |
| 125 | raise ValueError("syntax error") |
| 126 | |
| 127 | # Edge Case: Invalid syntax - consecutive operators or numbers |
| 128 | result = tokens[0] |
| 129 | i = 1 |
| 130 | while i < len(tokens): |
| 131 | # Expect an operator |
| 132 | if not isinstance(tokens[i], str) or tokens[i] not in '+-*/': |
| 133 | raise ValueError("syntax error") |
| 134 | |
| 135 | # Expect a number next |
| 136 | if i + 1 >= len(tokens) or not isinstance(tokens[i + 1], int): |
| 137 | raise ValueError("syntax error") |
| 138 | |
| 139 | operator = tokens[i] |
| 140 | operand = tokens[i + 1] |
| 141 | |
| 142 | if operator == '+': |
| 143 | result += operand |
| 144 | elif operator == '-': |
| 145 | result -= operand |
| 146 | elif operator == '*': |
| 147 | result *= operand |
| 148 | elif operator == '/': |
| 149 | # Edge Case: Division by zero |
| 150 | if operand == 0: |
| 151 | raise ValueError("syntax error") |
| 152 | result //= operand # Integer division as per examples |
| 153 | |
| 154 | i += 2 |
| 155 | |
| 156 | return result |
| 157 | # Handled Edge Cases: Empty input, non-math questions, missing question mark, |
| 158 | # empty expression, invalid characters, negative numbers, |
| 159 | # expression starting/ending with operator, single number, |
| 160 | # consecutive operators/numbers, division by zero, |
| 161 | # unsupported operations, malformed "multiplied by"/"divided by" |