| 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: 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:].rstrip('?') |
| 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 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': |
| 65 | # Edge Case: "multiplied" not followed by "by" |
| 66 | if i + 1 >= len(tokens) or tokens[i + 1] != 'by': |
| 67 | raise ValueError("syntax error") |
| 68 | parsed_tokens.append(operations[token]) |
| 69 | i += 1 # Skip the "by" token |
| 70 | elif token == 'divided': |
| 71 | # Edge Case: "divided" not followed by "by" |
| 72 | if i + 1 >= len(tokens) or tokens[i + 1] != 'by': |
| 73 | raise ValueError("syntax error") |
| 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 not parsed_tokens: |
| 86 | raise ValueError("syntax error") |
| 87 | |
| 88 | if isinstance(parsed_tokens[0], str): |
| 89 | raise ValueError("syntax error") |
| 90 | |
| 91 | # Evaluate the expression from left to right |
| 92 | result = parsed_tokens[0] |
| 93 | i = 1 |
| 94 | |
| 95 | # Edge Case: Single number with no operations |
| 96 | if len(parsed_tokens) == 1: |
| 97 | return result |
| 98 | |
| 99 | # Edge Case: Odd number of tokens means incomplete expression |
| 100 | if len(parsed_tokens) % 2 == 0: |
| 101 | raise ValueError("syntax error") |
| 102 | |
| 103 | while i < len(parsed_tokens): |
| 104 | # Edge Case: Expected operator but found number |
| 105 | if not isinstance(parsed_tokens[i], str): |
| 106 | raise ValueError("syntax error") |
| 107 | |
| 108 | operator = parsed_tokens[i] |
| 109 | |
| 110 | # Edge Case: Expected number but found operator |
| 111 | if i + 1 >= len(parsed_tokens) or isinstance(parsed_tokens[i + 1], str): |
| 112 | raise ValueError("syntax error") |
| 113 | |
| 114 | operand = parsed_tokens[i + 1] |
| 115 | |
| 116 | if operator == '+': |
| 117 | result += operand |
| 118 | elif operator == '-': |
| 119 | result -= operand |
| 120 | elif operator == '*': |
| 121 | result *= operand |
| 122 | elif operator == '/': |
| 123 | # Edge Case: Division by zero |
| 124 | if operand == 0: |
| 125 | raise ValueError("syntax error") # Following test expectations |
| 126 | result //= operand # Integer division as per examples |
| 127 | else: |
| 128 | # Edge Case: Unsupported operator (should not happen with current logic) |
| 129 | raise ValueError("unknown operation") |
| 130 | |
| 131 | i += 2 |
| 132 | |
| 133 | return result |
| 134 | # Handled Edge Cases: Empty input, non-math questions, missing question mark, |
| 135 | # unknown operations, syntax errors (missing 'by', |
| 136 | # consecutive operators/numbers, division by zero), |
| 137 | # single numbers, odd token counts, invalid expression structure |