| 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 "?" |
| 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 | # Tokenize the expression |
| 27 | tokens = [] |
| 28 | i = 0 |
| 29 | while i < len(expression): |
| 30 | if expression[i].isspace(): |
| 31 | i += 1 |
| 32 | continue |
| 33 | elif expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()): |
| 34 | # Parse number (including negative numbers) |
| 35 | start = i |
| 36 | if expression[i] == '-': |
| 37 | i += 1 |
| 38 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 39 | i += 1 |
| 40 | # Edge Case: Numbers with decimal points |
| 41 | num_str = expression[start:i] |
| 42 | if '.' in num_str: |
| 43 | # Check if it's a valid integer (e.g., "5.0") |
| 44 | try: |
| 45 | num = float(num_str) |
| 46 | if num != int(num): |
| 47 | raise ValueError("syntax error") |
| 48 | tokens.append(int(num)) |
| 49 | except ValueError: |
| 50 | raise ValueError("syntax error") |
| 51 | else: |
| 52 | tokens.append(int(num_str)) |
| 53 | elif expression[i].isalpha(): |
| 54 | # Parse word operators |
| 55 | start = i |
| 56 | while i < len(expression) and expression[i].isalpha(): |
| 57 | i += 1 |
| 58 | word = expression[start:i] |
| 59 | if word == "plus": |
| 60 | tokens.append("+") |
| 61 | elif word == "minus": |
| 62 | tokens.append("-") |
| 63 | elif word == "multiplied": |
| 64 | # Check for "by" after "multiplied" |
| 65 | if i >= len(expression) or not expression[i:].startswith(" by"): |
| 66 | raise ValueError("syntax error") |
| 67 | i += 3 # Skip " by" |
| 68 | tokens.append("*") |
| 69 | elif word == "divided": |
| 70 | # Check for "by" after "divided" |
| 71 | if i >= len(expression) or not expression[i:].startswith(" by"): |
| 72 | raise ValueError("syntax error") |
| 73 | i += 3 # Skip " by" |
| 74 | tokens.append("/") |
| 75 | else: |
| 76 | # Edge Case: Unknown operations |
| 77 | raise ValueError("unknown operation") |
| 78 | else: |
| 79 | # Edge Case: Invalid characters |
| 80 | raise ValueError("syntax error") |
| 81 | |
| 82 | # Edge Case: Expression with only one token (a number) |
| 83 | if len(tokens) == 1 and isinstance(tokens[0], int): |
| 84 | return tokens[0] |
| 85 | |
| 86 | # Edge Case: Invalid syntax (even number of tokens, or starts/ends with operator) |
| 87 | if len(tokens) < 3 or len(tokens) % 2 == 0 or not isinstance(tokens[0], int) or not isinstance(tokens[-1], int): |
| 88 | raise ValueError("syntax error") |
| 89 | |
| 90 | # Evaluate the expression from left to right |
| 91 | result = tokens[0] |
| 92 | i = 1 |
| 93 | while i < len(tokens): |
| 94 | # Edge Case: Invalid operator |
| 95 | if not isinstance(tokens[i], str): |
| 96 | raise ValueError("syntax error") |
| 97 | |
| 98 | operator = tokens[i] |
| 99 | # Edge Case: Missing operand |
| 100 | if i + 1 >= len(tokens) or not isinstance(tokens[i + 1], int): |
| 101 | raise ValueError("syntax error") |
| 102 | |
| 103 | operand = tokens[i + 1] |
| 104 | |
| 105 | if operator == "+": |
| 106 | result += operand |
| 107 | elif operator == "-": |
| 108 | result -= operand |
| 109 | elif operator == "*": |
| 110 | result *= operand |
| 111 | elif operator == "/": |
| 112 | # Edge Case: Division by zero |
| 113 | if operand == 0: |
| 114 | raise ValueError("syntax error") # Or we could raise ZeroDivisionError |
| 115 | result //= operand # Integer division as per examples |
| 116 | else: |
| 117 | # Edge Case: Unknown operator (should not happen with current parsing) |
| 118 | raise ValueError("unknown operation") |
| 119 | |
| 120 | i += 2 |
| 121 | |
| 122 | return result |
| 123 | # Handled Edge Cases: Non-math questions, empty expression, numbers with decimal points, |
| 124 | # unknown operations, invalid characters, expression with only one number, |
| 125 | # invalid syntax (even tokens, starts/ends with operator), invalid operators, |
| 126 | # missing operands, division by zero |