| 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, or is not a math question |
| 13 | """ |
| 14 | # Edge Case: Non-math questions |
| 15 | if not question.startswith("What is"): |
| 16 | raise ValueError("unknown operation") |
| 17 | |
| 18 | # Remove the "What is" prefix and the trailing question mark |
| 19 | expression = question[8:-1].strip() |
| 20 | |
| 21 | # Edge Case: Empty expression after "What is" |
| 22 | if not expression: |
| 23 | raise ValueError("syntax error") |
| 24 | |
| 25 | # Tokenize the expression |
| 26 | tokens = expression.split() |
| 27 | |
| 28 | # Edge Case: Expression with just a number |
| 29 | if len(tokens) == 1: |
| 30 | try: |
| 31 | return int(tokens[0]) |
| 32 | except ValueError: |
| 33 | # Edge Case: Single token that is not a number |
| 34 | raise ValueError("syntax error") |
| 35 | |
| 36 | # Convert tokens to numbers and operators |
| 37 | parsed_tokens = [] |
| 38 | i = 0 |
| 39 | while i < len(tokens): |
| 40 | token = tokens[i] |
| 41 | |
| 42 | # Try to parse as number |
| 43 | try: |
| 44 | num = int(token) |
| 45 | parsed_tokens.append(num) |
| 46 | i += 1 |
| 47 | continue |
| 48 | except ValueError: |
| 49 | pass |
| 50 | |
| 51 | # Handle operators |
| 52 | if token == "plus": |
| 53 | parsed_tokens.append("+") |
| 54 | elif token == "minus": |
| 55 | parsed_tokens.append("-") |
| 56 | elif token == "multiplied" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 57 | parsed_tokens.append("*") |
| 58 | i += 1 # Skip "by" |
| 59 | elif token == "divided" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 60 | parsed_tokens.append("/") |
| 61 | i += 1 # Skip "by" |
| 62 | else: |
| 63 | # Edge Case: Unknown operations |
| 64 | raise ValueError("unknown operation") |
| 65 | |
| 66 | i += 1 |
| 67 | |
| 68 | # Edge Case: Invalid syntax - operators without numbers |
| 69 | if len(parsed_tokens) < 3: |
| 70 | raise ValueError("syntax error") |
| 71 | |
| 72 | # Edge Case: Even number of tokens (should be odd: number, operator, number, ...) |
| 73 | if len(parsed_tokens) % 2 == 0: |
| 74 | raise ValueError("syntax error") |
| 75 | |
| 76 | # Validate alternating pattern: number, operator, number, operator, number... |
| 77 | for i in range(len(parsed_tokens)): |
| 78 | if i % 2 == 0: # Even indices should be numbers |
| 79 | if not isinstance(parsed_tokens[i], int): |
| 80 | # Edge Case: Expected number but found operator |
| 81 | raise ValueError("syntax error") |
| 82 | else: # Odd indices should be operators |
| 83 | if not isinstance(parsed_tokens[i], str): |
| 84 | # Edge Case: Expected operator but found number |
| 85 | raise ValueError("syntax error") |
| 86 | |
| 87 | # Evaluate left to right |
| 88 | result = parsed_tokens[0] |
| 89 | |
| 90 | # Edge Case: Division by zero |
| 91 | for i in range(1, len(parsed_tokens), 2): |
| 92 | operator = parsed_tokens[i] |
| 93 | operand = parsed_tokens[i + 1] |
| 94 | |
| 95 | if operator == "+": |
| 96 | result += operand |
| 97 | elif operator == "-": |
| 98 | result -= operand |
| 99 | elif operator == "*": |
| 100 | result *= operand |
| 101 | elif operator == "/": |
| 102 | if operand == 0: |
| 103 | raise ValueError("syntax error") # Division by zero |
| 104 | result //= operand # Integer division as per examples |
| 105 | |
| 106 | return result |
| 107 | # Handled Edge Cases: Non-math questions, empty expressions, single numbers, unknown operations, |
| 108 | # invalid syntax (operators without numbers, wrong token pattern), division by zero |