| 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 | # 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 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 | try: |
| 44 | num = float(num_str) |
| 45 | # Edge Case: Non-integer floats |
| 46 | if not num.is_integer(): |
| 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 | else: |
| 54 | # Parse 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): |
| 66 | # Skip spaces |
| 67 | while i < len(expression) and expression[i].isspace(): |
| 68 | i += 1 |
| 69 | # Check for "by" |
| 70 | if i + 1 < len(expression) and expression[i:i+2] == "by": |
| 71 | # Skip "by" |
| 72 | i += 2 |
| 73 | tokens.append("*") |
| 74 | else: |
| 75 | raise ValueError("syntax error") |
| 76 | else: |
| 77 | raise ValueError("syntax error") |
| 78 | elif word == "divided": |
| 79 | # Check for "by" after "divided" |
| 80 | if i < len(expression): |
| 81 | # Skip spaces |
| 82 | while i < len(expression) and expression[i].isspace(): |
| 83 | i += 1 |
| 84 | # Check for "by" |
| 85 | if i + 1 < len(expression) and expression[i:i+2] == "by": |
| 86 | # Skip "by" |
| 87 | i += 2 |
| 88 | tokens.append("/") |
| 89 | else: |
| 90 | raise ValueError("syntax error") |
| 91 | else: |
| 92 | raise ValueError("syntax error") |
| 93 | else: |
| 94 | # Edge Case: Unknown operations |
| 95 | raise ValueError("unknown operation") |
| 96 | |
| 97 | # Edge Case: Expression ending with an operator |
| 98 | if not tokens: |
| 99 | raise ValueError("syntax error") |
| 100 | |
| 101 | # Edge Case: Expression ending with an operator |
| 102 | if isinstance(tokens[-1], str): |
| 103 | raise ValueError("syntax error") |
| 104 | |
| 105 | # Evaluate the expression from left to right |
| 106 | # Edge Case: Starting with an operator |
| 107 | if isinstance(tokens[0], str): |
| 108 | raise ValueError("syntax error") |
| 109 | |
| 110 | result = tokens[0] |
| 111 | i = 1 |
| 112 | |
| 113 | while i < len(tokens): |
| 114 | # Edge Case: Consecutive operators |
| 115 | if isinstance(tokens[i], str): |
| 116 | if i + 1 >= len(tokens): |
| 117 | # Edge Case: Operator at the end |
| 118 | raise ValueError("syntax error") |
| 119 | # Edge Case: Next token is not a number |
| 120 | if not isinstance(tokens[i+1], int): |
| 121 | raise ValueError("syntax error") |
| 122 | |
| 123 | operator = tokens[i] |
| 124 | operand = tokens[i+1] |
| 125 | |
| 126 | if operator == "+": |
| 127 | result += operand |
| 128 | elif operator == "-": |
| 129 | result -= operand |
| 130 | elif operator == "*": |
| 131 | result *= operand |
| 132 | elif operator == "/": |
| 133 | # Edge Case: Division by zero |
| 134 | if operand == 0: |
| 135 | raise ValueError("syntax error") # Or we could raise ZeroDivisionError |
| 136 | result //= operand # Integer division as per examples |
| 137 | else: |
| 138 | # This shouldn't happen due to earlier validation |
| 139 | raise ValueError("unknown operation") |
| 140 | |
| 141 | i += 2 |
| 142 | else: |
| 143 | # Edge Case: Consecutive numbers without operator |
| 144 | raise ValueError("syntax error") |
| 145 | |
| 146 | return result |
| 147 | # Handled Edge Cases: Non-math questions, empty expression, numbers with decimal points, non-integer floats, expression ending with an operator, starting with an operator, consecutive operators, operator at the end, next token is not a number, division by zero, consecutive numbers without operator, unknown operations |