| 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: 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 '?') |
| 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 = expression.split() |
| 36 | |
| 37 | # Edge Case: No tokens |
| 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 operators |
| 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 | num = int(token) |
| 59 | parsed_tokens.append(num) |
| 60 | except ValueError: |
| 61 | # Not a number, check if it's a known operation |
| 62 | if token in operations: |
| 63 | # Special handling for "multiplied by" and "divided by" |
| 64 | if token == "multiplied" or token == "divided": |
| 65 | # Edge Case: Incomplete "multiplied" or "divided" operation |
| 66 | if i + 1 >= len(tokens): |
| 67 | raise ValueError("syntax error") |
| 68 | |
| 69 | next_token = tokens[i + 1] |
| 70 | if (token == "multiplied" and next_token != "by") or \ |
| 71 | (token == "divided" and next_token != "by"): |
| 72 | raise ValueError("syntax error") |
| 73 | |
| 74 | parsed_tokens.append(operations[token]) |
| 75 | i += 1 # Skip the "by" token |
| 76 | else: |
| 77 | parsed_tokens.append(operations[token]) |
| 78 | else: |
| 79 | # Edge Case: Unknown operation |
| 80 | raise ValueError("unknown operation") |
| 81 | |
| 82 | i += 1 |
| 83 | |
| 84 | # Edge Case: Expression starts with an operator |
| 85 | if len(parsed_tokens) > 0 and isinstance(parsed_tokens[0], str): |
| 86 | raise ValueError("syntax error") |
| 87 | |
| 88 | # Edge Case: Expression ends with an operator |
| 89 | if len(parsed_tokens) > 0 and isinstance(parsed_tokens[-1], str): |
| 90 | raise ValueError("syntax error") |
| 91 | |
| 92 | # Edge Case: Invalid pattern (operator followed by operator) |
| 93 | for i in range(len(parsed_tokens) - 1): |
| 94 | if isinstance(parsed_tokens[i], str) and isinstance(parsed_tokens[i + 1], str): |
| 95 | raise ValueError("syntax error") |
| 96 | |
| 97 | # Edge Case: Invalid pattern (number followed by number) |
| 98 | for i in range(len(parsed_tokens) - 1): |
| 99 | if isinstance(parsed_tokens[i], int) and isinstance(parsed_tokens[i + 1], int): |
| 100 | raise ValueError("syntax error") |
| 101 | |
| 102 | # Evaluate the expression from left to right |
| 103 | # Edge Case: Single number |
| 104 | if len(parsed_tokens) == 1: |
| 105 | return parsed_tokens[0] |
| 106 | |
| 107 | # Edge Case: Invalid length (should be odd number of tokens) |
| 108 | if len(parsed_tokens) % 2 == 0: |
| 109 | raise ValueError("syntax error") |
| 110 | |
| 111 | result = parsed_tokens[0] |
| 112 | |
| 113 | # Process operations from left to right |
| 114 | for i in range(1, len(parsed_tokens), 2): |
| 115 | operator = parsed_tokens[i] |
| 116 | operand = parsed_tokens[i + 1] |
| 117 | |
| 118 | if operator == "+": |
| 119 | result += operand |
| 120 | elif operator == "-": |
| 121 | result -= operand |
| 122 | elif operator == "*": |
| 123 | result *= operand |
| 124 | elif operator == "/": |
| 125 | # Edge Case: Division by zero |
| 126 | if operand == 0: |
| 127 | raise ValueError("syntax error") |
| 128 | result //= operand # Integer division as per examples |
| 129 | |
| 130 | return result |
| 131 | # Handled Edge Cases: Empty input, None input, questions not starting with "What is", |
| 132 | # questions not ending with '?', empty expressions, |
| 133 | # unknown operations, incomplete "multiplied/divided" operations, |
| 134 | # expressions starting/ending with operators, |
| 135 | # operator-operator or number-number sequences, |
| 136 | # single numbers, invalid token sequence lengths, |
| 137 | # division by zero |