| 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: Non-math questions |
| 20 | if not question.startswith("What is"): |
| 21 | raise ValueError("unknown operation") |
| 22 | |
| 23 | # Edge Case: Malformed questions without a question mark |
| 24 | if not question.endswith("?"): |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Remove "What is" and "?" to get the expression |
| 28 | expression = question[8:-1].strip() |
| 29 | |
| 30 | # Edge Case: Empty expression after removing "What is" and "?" |
| 31 | if not expression: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | # Tokenize the expression |
| 35 | tokens = expression.split() |
| 36 | |
| 37 | # Edge Case: Expression with 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 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 | # 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 | # Handle operations |
| 77 | if token in operations: |
| 78 | if token == "multiplied" or token == "divided": |
| 79 | # These operations require "by" after them |
| 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 | else: |
| 88 | # Edge Case: Unknown operations |
| 89 | raise ValueError("unknown operation") |
| 90 | |
| 91 | # Edge Case: Expression with only one token that's not a number |
| 92 | if len(parsed_tokens) == 1 and not isinstance(parsed_tokens[0], int): |
| 93 | raise ValueError("syntax error") |
| 94 | |
| 95 | # Edge Case: Even number of tokens (should be odd: number, operator, number, ...) |
| 96 | if len(parsed_tokens) % 2 == 0: |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | # Evaluate the expression from left to right |
| 100 | result = parsed_tokens[0] |
| 101 | |
| 102 | # Edge Case: First token is not a number |
| 103 | if not isinstance(result, int): |
| 104 | raise ValueError("syntax error") |
| 105 | |
| 106 | i = 1 |
| 107 | while i < len(parsed_tokens): |
| 108 | # Edge Case: Missing operator |
| 109 | if i + 1 >= len(parsed_tokens): |
| 110 | raise ValueError("syntax error") |
| 111 | |
| 112 | operator = parsed_tokens[i] |
| 113 | operand = parsed_tokens[i + 1] |
| 114 | |
| 115 | # Edge Case: Invalid operator type |
| 116 | if not isinstance(operator, str): |
| 117 | raise ValueError("syntax error") |
| 118 | |
| 119 | # Edge Case: Invalid operand type |
| 120 | if not isinstance(operand, int): |
| 121 | raise ValueError("syntax error") |
| 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") |
| 133 | result //= operand # Integer division as per examples |
| 134 | else: |
| 135 | # Edge Case: Unknown operator |
| 136 | raise ValueError("unknown operation") |
| 137 | |
| 138 | i += 2 |
| 139 | |
| 140 | return result |
| 141 | # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark, |
| 142 | # empty expression, no tokens, unknown operations, negative numbers, |
| 143 | # missing 'by' after multiplied/divided, even number of tokens, |
| 144 | # first token not a number, missing operators, invalid operator/operand types, |
| 145 | # division by zero, malformed expressions |