| 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 | An integer 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 string 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: Question is just "What is" with no numbers or operations |
| 24 | if question == "What is": |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Remove the "What is " prefix and the trailing question mark |
| 28 | expression = question[8:-1].strip() |
| 29 | |
| 30 | # Edge Case: Empty expression after removing prefix and question mark |
| 31 | if not expression: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | # Split the expression into tokens |
| 35 | tokens = expression.split() |
| 36 | |
| 37 | # Edge Case: No tokens after splitting |
| 38 | if not tokens: |
| 39 | raise ValueError("syntax error") |
| 40 | |
| 41 | # Define operation mappings |
| 42 | operations = { |
| 43 | "plus": "+", |
| 44 | "minus": "-", |
| 45 | "multiplied": "*", |
| 46 | "divided": "/" |
| 47 | } |
| 48 | |
| 49 | # Parse tokens into a list of numbers and operations |
| 50 | parsed_tokens = [] |
| 51 | i = 0 |
| 52 | |
| 53 | while i < len(tokens): |
| 54 | token = tokens[i] |
| 55 | |
| 56 | # Try to parse as number |
| 57 | try: |
| 58 | # Handle negative numbers |
| 59 | if token == "-" and i + 1 < len(tokens): |
| 60 | # Check if next token is a number |
| 61 | next_token = tokens[i + 1] |
| 62 | try: |
| 63 | num = int(next_token) |
| 64 | parsed_tokens.append(-num) |
| 65 | i += 2 # Skip both the minus and the number |
| 66 | continue |
| 67 | except ValueError: |
| 68 | pass # Not a number, treat minus as operation |
| 69 | |
| 70 | num = int(token) |
| 71 | parsed_tokens.append(num) |
| 72 | i += 1 |
| 73 | continue |
| 74 | except ValueError: |
| 75 | pass # Not a number, check for operations |
| 76 | |
| 77 | # Check for operations |
| 78 | if token in operations: |
| 79 | # Special handling for "multiplied by" and "divided by" |
| 80 | if token == "multiplied" or token == "divided": |
| 81 | # Edge Case: Missing "by" after "multiplied" or "divided" |
| 82 | if i + 1 >= len(tokens) or tokens[i + 1] != "by": |
| 83 | raise ValueError("syntax error") |
| 84 | parsed_tokens.append(operations[token]) |
| 85 | i += 2 # Skip both the operation and "by" |
| 86 | else: |
| 87 | parsed_tokens.append(operations[token]) |
| 88 | i += 1 |
| 89 | else: |
| 90 | # Edge Case: Unknown operation |
| 91 | raise ValueError("unknown operation") |
| 92 | |
| 93 | # Edge Case: Expression ends with an operation |
| 94 | if len(parsed_tokens) > 0 and isinstance(parsed_tokens[-1], str): |
| 95 | raise ValueError("syntax error") |
| 96 | |
| 97 | # Edge Case: Expression starts with an operation |
| 98 | if len(parsed_tokens) > 0 and isinstance(parsed_tokens[0], str): |
| 99 | raise ValueError("syntax error") |
| 100 | |
| 101 | # Edge Case: Invalid pattern (operation followed by operation) |
| 102 | for i in range(len(parsed_tokens) - 1): |
| 103 | if isinstance(parsed_tokens[i], str) and isinstance(parsed_tokens[i + 1], str): |
| 104 | raise ValueError("syntax error") |
| 105 | |
| 106 | # Edge Case: Invalid pattern (number followed by number) |
| 107 | for i in range(len(parsed_tokens) - 1): |
| 108 | if isinstance(parsed_tokens[i], int) and isinstance(parsed_tokens[i + 1], int): |
| 109 | raise ValueError("syntax error") |
| 110 | |
| 111 | # Evaluate the expression from left to right |
| 112 | # Edge Case: Single number |
| 113 | if len(parsed_tokens) == 1: |
| 114 | return parsed_tokens[0] |
| 115 | |
| 116 | # Edge Case: Invalid length (should be odd number of tokens) |
| 117 | if len(parsed_tokens) % 2 == 0: |
| 118 | raise ValueError("syntax error") |
| 119 | |
| 120 | result = parsed_tokens[0] |
| 121 | i = 1 |
| 122 | |
| 123 | while i < len(parsed_tokens): |
| 124 | operation = parsed_tokens[i] |
| 125 | # Edge Case: Missing operand |
| 126 | if i + 1 >= len(parsed_tokens): |
| 127 | raise ValueError("syntax error") |
| 128 | |
| 129 | operand = parsed_tokens[i + 1] |
| 130 | |
| 131 | if operation == "+": |
| 132 | result += operand |
| 133 | elif operation == "-": |
| 134 | result -= operand |
| 135 | elif operation == "*": |
| 136 | result *= operand |
| 137 | elif operation == "/": |
| 138 | # Edge Case: Division by zero |
| 139 | if operand == 0: |
| 140 | raise ValueError("syntax error") # Or we could raise ZeroDivisionError |
| 141 | result //= operand # Integer division as per examples |
| 142 | else: |
| 143 | # This shouldn't happen with our parsing, but just in case |
| 144 | raise ValueError("unknown operation") |
| 145 | |
| 146 | i += 2 |
| 147 | |
| 148 | return result |
| 149 | # Handled Edge Cases: Empty input, non-math questions, missing question mark, |
| 150 | # just "What is", missing operations, unknown operations, |
| 151 | # negative numbers, invalid syntax patterns, division by zero, |
| 152 | # missing operands, single number, invalid token sequences |