| 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 unsupported operations, |
| 13 | or is not a math question. |
| 14 | """ |
| 15 | # Edge Case: Non-math questions |
| 16 | if not question.startswith("What is"): |
| 17 | raise ValueError("unknown operation") |
| 18 | |
| 19 | # Remove the "What is" prefix and the trailing question mark |
| 20 | expression = question[8:-1].strip() |
| 21 | |
| 22 | # Edge Case: Empty expression after "What is" |
| 23 | if not expression: |
| 24 | raise ValueError("syntax error") |
| 25 | |
| 26 | # Split the expression into tokens |
| 27 | tokens = expression.split() |
| 28 | |
| 29 | # Edge Case: Invalid syntax with no numbers |
| 30 | if not tokens: |
| 31 | raise ValueError("syntax error") |
| 32 | |
| 33 | # Define operation mappings |
| 34 | operations = { |
| 35 | "plus": "+", |
| 36 | "minus": "-", |
| 37 | "multiplied": "*", |
| 38 | "divided": "/" |
| 39 | } |
| 40 | |
| 41 | # Parse the tokens |
| 42 | parsed_tokens = [] |
| 43 | i = 0 |
| 44 | while i < len(tokens): |
| 45 | token = tokens[i] |
| 46 | |
| 47 | # Try to parse as number |
| 48 | try: |
| 49 | num = int(token) |
| 50 | parsed_tokens.append(num) |
| 51 | except ValueError: |
| 52 | # Handle operations |
| 53 | if token in operations: |
| 54 | # Special handling for "multiplied by" and "divided by" |
| 55 | if token == "multiplied" or token == "divided": |
| 56 | # Edge Case: Incomplete "multiplied by" or "divided by" expressions |
| 57 | if i + 1 >= len(tokens): |
| 58 | raise ValueError("syntax error") |
| 59 | |
| 60 | next_token = tokens[i + 1] |
| 61 | if (token == "multiplied" and next_token != "by") or \ |
| 62 | (token == "divided" and next_token != "by"): |
| 63 | raise ValueError("syntax error") |
| 64 | |
| 65 | parsed_tokens.append(operations[token]) |
| 66 | i += 1 # Skip the "by" token |
| 67 | else: |
| 68 | parsed_tokens.append(operations[token]) |
| 69 | else: |
| 70 | # Edge Case: Unsupported operations |
| 71 | raise ValueError("unknown operation") |
| 72 | |
| 73 | i += 1 |
| 74 | |
| 75 | # Edge Case: Expression ending with an operator |
| 76 | if len(parsed_tokens) % 2 == 0: |
| 77 | raise ValueError("syntax error") |
| 78 | |
| 79 | # Evaluate the expression from left to right |
| 80 | result = parsed_tokens[0] |
| 81 | |
| 82 | # Edge Case: Single number with no operations |
| 83 | if len(parsed_tokens) == 1: |
| 84 | return result |
| 85 | |
| 86 | # Process operations in pairs |
| 87 | i = 1 |
| 88 | while i < len(parsed_tokens): |
| 89 | # Edge Case: Invalid syntax with consecutive operators or numbers |
| 90 | if not isinstance(parsed_tokens[i], str): |
| 91 | raise ValueError("syntax error") |
| 92 | |
| 93 | if i + 1 >= len(parsed_tokens): |
| 94 | raise ValueError("syntax error") |
| 95 | |
| 96 | if not isinstance(parsed_tokens[i + 1], int): |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | operator = parsed_tokens[i] |
| 100 | operand = parsed_tokens[i + 1] |
| 101 | |
| 102 | if operator == "+": |
| 103 | result += operand |
| 104 | elif operator == "-": |
| 105 | result -= operand |
| 106 | elif operator == "*": |
| 107 | result *= operand |
| 108 | elif operator == "/": |
| 109 | # Edge Case: Division by zero |
| 110 | if operand == 0: |
| 111 | raise ValueError("syntax error") |
| 112 | result //= operand # Integer division as per examples |
| 113 | else: |
| 114 | # Edge Case: Unknown operator (should not happen with current parsing) |
| 115 | raise ValueError("unknown operation") |
| 116 | |
| 117 | i += 2 |
| 118 | |
| 119 | return result |
| 120 | # Handled Edge Cases: Non-math questions, empty expressions, invalid syntax with no numbers, |
| 121 | # incomplete "multiplied by" or "divided by" expressions, unsupported operations, |
| 122 | # expression ending with an operator, single number with no operations, |
| 123 | # invalid syntax with consecutive operators or numbers, division by zero, |
| 124 | # unknown operator |