| 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 unknown 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 | # 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 | # Handle 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_val = float(num_str) |
| 46 | if not num_val.is_integer(): |
| 47 | raise ValueError("syntax error") |
| 48 | tokens.append(int(num_val)) |
| 49 | except ValueError: |
| 50 | raise ValueError("syntax error") |
| 51 | else: |
| 52 | tokens.append(int(num_str)) |
| 53 | else: |
| 54 | # Handle operators and words |
| 55 | start = i |
| 56 | while i < len(expression) and not expression[i].isspace(): |
| 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:].lstrip().startswith("by"): |
| 66 | raise ValueError("syntax error") |
| 67 | # Skip "by" in the expression |
| 68 | j = i |
| 69 | while j < len(expression) and expression[j].isspace(): |
| 70 | j += 1 |
| 71 | j += 2 # Skip "by" |
| 72 | i = j |
| 73 | tokens.append("*") |
| 74 | elif word == "divided": |
| 75 | # Check for "by" after "divided" |
| 76 | if i >= len(expression) or not expression[i:].lstrip().startswith("by"): |
| 77 | raise ValueError("syntax error") |
| 78 | # Skip "by" in the expression |
| 79 | j = i |
| 80 | while j < len(expression) and expression[j].isspace(): |
| 81 | j += 1 |
| 82 | j += 2 # Skip "by" |
| 83 | i = j |
| 84 | tokens.append("/") |
| 85 | else: |
| 86 | # Edge Case: Unknown operations |
| 87 | raise ValueError("unknown operation") |
| 88 | |
| 89 | # Edge Case: Expression ending with an operator |
| 90 | if not tokens: |
| 91 | raise ValueError("syntax error") |
| 92 | |
| 93 | # Edge Case: Expression with only one number |
| 94 | if len(tokens) == 1: |
| 95 | if isinstance(tokens[0], int): |
| 96 | return tokens[0] |
| 97 | else: |
| 98 | raise ValueError("syntax error") |
| 99 | |
| 100 | # Edge Case: Expression starting with an operator |
| 101 | if not isinstance(tokens[0], int): |
| 102 | raise ValueError("syntax error") |
| 103 | |
| 104 | # Evaluate the expression from left to right |
| 105 | result = tokens[0] |
| 106 | i = 1 |
| 107 | |
| 108 | # Edge Case: Invalid syntax with operators |
| 109 | while i < len(tokens): |
| 110 | if i + 1 >= len(tokens): |
| 111 | raise ValueError("syntax error") |
| 112 | |
| 113 | if not isinstance(tokens[i], str) or not isinstance(tokens[i+1], int): |
| 114 | raise ValueError("syntax error") |
| 115 | |
| 116 | operator = tokens[i] |
| 117 | operand = tokens[i+1] |
| 118 | |
| 119 | if operator == "+": |
| 120 | result += operand |
| 121 | elif operator == "-": |
| 122 | result -= operand |
| 123 | elif operator == "*": |
| 124 | result *= operand |
| 125 | elif operator == "/": |
| 126 | # Edge Case: Division by zero |
| 127 | if operand == 0: |
| 128 | raise ValueError("syntax error") # Or we could raise ZeroDivisionError |
| 129 | result //= operand # Integer division as per examples |
| 130 | else: |
| 131 | # This shouldn't happen with our parsing, but just in case |
| 132 | raise ValueError("unknown operation") |
| 133 | |
| 134 | i += 2 |
| 135 | |
| 136 | return result |
| 137 | # Handled Edge Cases: Non-math questions, empty expressions, numbers with decimal points, |
| 138 | # expression ending with operator, expression with only one number, |
| 139 | # expression starting with operator, invalid syntax with operators, |
| 140 | # division by zero, unknown operations |