| 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: 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 | # Tokenize the expression |
| 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 | try: |
| 62 | num = int(tokens[i + 1]) |
| 63 | parsed_tokens.append(-num) |
| 64 | i += 2 |
| 65 | continue |
| 66 | except ValueError: |
| 67 | pass |
| 68 | |
| 69 | num = int(token) |
| 70 | parsed_tokens.append(num) |
| 71 | i += 1 |
| 72 | continue |
| 73 | except ValueError: |
| 74 | pass |
| 75 | |
| 76 | # Check for operations |
| 77 | if token in operations: |
| 78 | if token == "multiplied" or token == "divided": |
| 79 | # These operations require "by" as the next token |
| 80 | if i + 1 >= len(tokens) or tokens[i + 1] != "by": |
| 81 | raise ValueError("syntax error") |
| 82 | parsed_tokens.append(operations[token]) |
| 83 | i += 2 |
| 84 | else: |
| 85 | parsed_tokens.append(operations[token]) |
| 86 | i += 1 |
| 87 | continue |
| 88 | |
| 89 | # Edge Case: Unknown operation |
| 90 | raise ValueError("unknown operation") |
| 91 | |
| 92 | # Edge Case: Expression starts with an operation |
| 93 | if len(parsed_tokens) > 0 and isinstance(parsed_tokens[0], str): |
| 94 | raise ValueError("syntax error") |
| 95 | |
| 96 | # Edge Case: Expression ends with an operation |
| 97 | if len(parsed_tokens) > 0 and isinstance(parsed_tokens[-1], str): |
| 98 | raise ValueError("syntax error") |
| 99 | |
| 100 | # Edge Case: Empty parsed tokens |
| 101 | if not parsed_tokens: |
| 102 | raise ValueError("syntax error") |
| 103 | |
| 104 | # Evaluate the expression from left to right |
| 105 | result = parsed_tokens[0] |
| 106 | i = 1 |
| 107 | |
| 108 | # Edge Case: Single number with no operations |
| 109 | if len(parsed_tokens) == 1: |
| 110 | return result |
| 111 | |
| 112 | # Edge Case: Invalid pattern (operation followed by operation) |
| 113 | while i < len(parsed_tokens): |
| 114 | if i + 1 >= len(parsed_tokens): |
| 115 | raise ValueError("syntax error") |
| 116 | |
| 117 | operation = parsed_tokens[i] |
| 118 | operand = parsed_tokens[i + 1] |
| 119 | |
| 120 | # Edge Case: Expecting operation but got number |
| 121 | if not isinstance(operation, str): |
| 122 | raise ValueError("syntax error") |
| 123 | |
| 124 | # Edge Case: Expecting number but got operation |
| 125 | if not isinstance(operand, int): |
| 126 | raise ValueError("syntax error") |
| 127 | |
| 128 | if operation == "+": |
| 129 | result += operand |
| 130 | elif operation == "-": |
| 131 | result -= operand |
| 132 | elif operation == "*": |
| 133 | result *= operand |
| 134 | elif operation == "/": |
| 135 | # Edge Case: Division by zero |
| 136 | if operand == 0: |
| 137 | raise ValueError("syntax error") |
| 138 | result //= operand # Integer division as per examples |
| 139 | else: |
| 140 | # Edge Case: Unknown operation in evaluation |
| 141 | raise ValueError("unknown operation") |
| 142 | |
| 143 | i += 2 |
| 144 | |
| 145 | return result |
| 146 | # Handled Edge Cases: Empty input, invalid question format, missing expression, |
| 147 | # negative numbers, unknown operations, syntax errors, |
| 148 | # division by zero, malformed expressions, missing operands |