| 1 | 1 | def answer(question: str) -> int: |
| 2 | | pass |
| 2 | """ |
| 3 | Parse and evaluate a simple math word problem, returning the answer as an integer. |
| 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, or is not a math question |
| 13 | """ |
| 14 | # Edge Case: Empty or None input |
| 15 | if not question: |
| 16 | raise ValueError("syntax error") |
| 17 | |
| 18 | # Edge Case: Question doesn't start with "What is" |
| 19 | if not question.startswith("What is"): |
| 20 | raise ValueError("syntax error") |
| 21 | |
| 22 | # Remove the "What is" prefix and the trailing question mark |
| 23 | expression = question[8:].rstrip('?') |
| 24 | |
| 25 | # Edge Case: Empty expression after removing prefix and question mark |
| 26 | if not expression: |
| 27 | raise ValueError("syntax error") |
| 28 | |
| 29 | # Split the expression into tokens |
| 30 | tokens = expression.split() |
| 31 | |
| 32 | # Edge Case: No tokens |
| 33 | if not tokens: |
| 34 | raise ValueError("syntax error") |
| 35 | |
| 36 | # Define supported operations |
| 37 | operations = { |
| 38 | 'plus': '+', |
| 39 | 'minus': '-', |
| 40 | 'multiplied': '*', |
| 41 | 'divided': '/' |
| 42 | } |
| 43 | |
| 44 | # Parse tokens into numbers and operators |
| 45 | parsed_tokens = [] |
| 46 | i = 0 |
| 47 | |
| 48 | while i < len(tokens): |
| 49 | token = tokens[i] |
| 50 | |
| 51 | # Try to parse as number |
| 52 | try: |
| 53 | # Handle negative numbers |
| 54 | if token == '-' and i + 1 < len(tokens): |
| 55 | # Check if next token is a number |
| 56 | try: |
| 57 | num = int(tokens[i + 1]) |
| 58 | parsed_tokens.append(-num) |
| 59 | i += 2 |
| 60 | continue |
| 61 | except ValueError: |
| 62 | pass |
| 63 | |
| 64 | num = int(token) |
| 65 | parsed_tokens.append(num) |
| 66 | i += 1 |
| 67 | continue |
| 68 | except ValueError: |
| 69 | pass |
| 70 | |
| 71 | # Handle operations |
| 72 | if token in operations: |
| 73 | if token == 'multiplied' or token == 'divided': |
| 74 | # These operations require the next word |
| 75 | # Edge Case: Incomplete operation (e.g., "multiplied" at end) |
| 76 | if i + 1 >= len(tokens): |
| 77 | raise ValueError("syntax error") |
| 78 | |
| 79 | # Edge Case: Incorrect operation format (e.g., "multiplied by" missing "by") |
| 80 | if (token == 'multiplied' and tokens[i + 1] != 'by') or \ |
| 81 | (token == 'divided' and tokens[i + 1] != 'by'): |
| 82 | raise ValueError("syntax error") |
| 83 | |
| 84 | parsed_tokens.append(operations[token]) |
| 85 | i += 2 |
| 86 | continue |
| 87 | else: |
| 88 | parsed_tokens.append(operations[token]) |
| 89 | i += 1 |
| 90 | continue |
| 91 | |
| 92 | # Edge Case: Unknown operation |
| 93 | raise ValueError("unknown operation") |
| 94 | |
| 95 | # Edge Case: Expression starts with an operator |
| 96 | if not parsed_tokens: |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | if isinstance(parsed_tokens[0], str): |
| 100 | raise ValueError("syntax error") |
| 101 | |
| 102 | # Evaluate the expression from left to right |
| 103 | result = parsed_tokens[0] |
| 104 | i = 1 |
| 105 | |
| 106 | # Edge Case: Odd number of tokens (invalid syntax) |
| 107 | if len(parsed_tokens) % 2 == 0: |
| 108 | raise ValueError("syntax error") |
| 109 | |
| 110 | while i < len(parsed_tokens): |
| 111 | # Edge Case: Expected operator but found number |
| 112 | if not isinstance(parsed_tokens[i], str): |
| 113 | raise ValueError("syntax error") |
| 114 | |
| 115 | operator = parsed_tokens[i] |
| 116 | |
| 117 | # Edge Case: Expected number but found operator |
| 118 | if i + 1 >= len(parsed_tokens) or isinstance(parsed_tokens[i + 1], str): |
| 119 | raise ValueError("syntax error") |
| 120 | |
| 121 | operand = parsed_tokens[i + 1] |
| 122 | |
| 123 | if operator == '+': |
| 124 | result += operand |
| 125 | elif operator == '-': |
| 126 | result -= operand |
| 127 | elif operator == '*': |
| 128 | result *= operand |
| 129 | elif operator == '/': |
| 130 | # Edge Case: Division by zero |
| 131 | if operand == 0: |
| 132 | raise ValueError("syntax error") # Or we could let Python raise ZeroDivisionError |
| 133 | result //= operand # Integer division as per examples |
| 134 | |
| 135 | i += 2 |
| 136 | |
| 137 | return result |
| 138 | # Handled Edge Cases: Empty input, None input, non-math questions, questions without 'What is', |
| 139 | # empty expressions, no tokens, unknown operations, incomplete operations, |
| 140 | # incorrect operation format, expression starting with operator, |
| 141 | # odd number of tokens, expected operator but found number, |
| 142 | # expected number but found operator, division by zero |