| 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: 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 after "What is" |
| 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 | i += 1 |
| 45 | if i < len(expression) and expression[i].isdigit(): |
| 46 | start = i - 1 |
| 47 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 48 | i += 1 |
| 49 | tokens.append(int(expression[start:i])) |
| 50 | else: |
| 51 | # This is a minus operator, not a negative number |
| 52 | tokens.append('-') |
| 53 | i -= 1 # Rewind to process the digit or space in next iteration |
| 54 | # Handle positive numbers |
| 55 | elif expression[i].isdigit(): |
| 56 | start = i |
| 57 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 58 | i += 1 |
| 59 | tokens.append(int(expression[start:i])) |
| 60 | # Handle operators |
| 61 | elif expression[i:].startswith('plus'): |
| 62 | tokens.append('+') |
| 63 | i += 4 |
| 64 | elif expression[i:].startswith('minus'): |
| 65 | tokens.append('-') |
| 66 | i += 5 |
| 67 | elif expression[i:].startswith('multiplied by'): |
| 68 | tokens.append('*') |
| 69 | i += 13 |
| 70 | elif expression[i:].startswith('divided by'): |
| 71 | tokens.append('/') |
| 72 | i += 10 |
| 73 | else: |
| 74 | # Edge Case: Unknown operation or invalid character |
| 75 | raise ValueError("unknown operation") |
| 76 | |
| 77 | # Edge Case: Empty tokens list |
| 78 | if not tokens: |
| 79 | raise ValueError("syntax error") |
| 80 | |
| 81 | # Edge Case: Expression starts with an operator (except minus for negative numbers) |
| 82 | if tokens[0] in ['+', '*', '/']: |
| 83 | raise ValueError("syntax error") |
| 84 | |
| 85 | # Edge Case: Expression ends with an operator |
| 86 | if tokens[-1] in ['+', '-', '*', '/']: |
| 87 | raise ValueError("syntax error") |
| 88 | |
| 89 | # Validate syntax - check alternating pattern of number and operator |
| 90 | for i in range(len(tokens)): |
| 91 | if i % 2 == 0: # Even positions should be numbers |
| 92 | if not isinstance(tokens[i], int): |
| 93 | # Edge Case: Two operators in a row |
| 94 | raise ValueError("syntax error") |
| 95 | else: # Odd positions should be operators |
| 96 | if tokens[i] not in ['+', '-', '*', '/']: |
| 97 | # Edge Case: Two numbers in a row |
| 98 | raise ValueError("syntax error") |
| 99 | |
| 100 | # Evaluate the expression from left to right |
| 101 | result = tokens[0] |
| 102 | i = 1 |
| 103 | while i < len(tokens): |
| 104 | operator = tokens[i] |
| 105 | # Edge Case: Not enough operands for an operation |
| 106 | if i + 1 >= len(tokens): |
| 107 | raise ValueError("syntax error") |
| 108 | |
| 109 | operand = tokens[i + 1] |
| 110 | |
| 111 | if operator == '+': |
| 112 | result += operand |
| 113 | elif operator == '-': |
| 114 | result -= operand |
| 115 | elif operator == '*': |
| 116 | result *= operand |
| 117 | elif operator == '/': |
| 118 | # Edge Case: Division by zero |
| 119 | if operand == 0: |
| 120 | raise ValueError("syntax error") |
| 121 | result //= operand # Integer division as per examples |
| 122 | |
| 123 | i += 2 |
| 124 | |
| 125 | return result |
| 126 | # Handled Edge Cases: Empty or None input, question doesn't start with "What is", |
| 127 | # malformed question, empty expression, unknown operations, |
| 128 | # invalid characters, expression starts/ends with operator, |
| 129 | # two operators in a row, two numbers in a row, not enough operands, |
| 130 | # division by zero |