| 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: Empty or None input |
| 16 | if not question: |
| 17 | raise ValueError("syntax error") |
| 18 | |
| 19 | # Edge Case: Non-math questions |
| 20 | if not question.startswith("What is"): |
| 21 | raise ValueError("unknown operation") |
| 22 | |
| 23 | # Edge Case: Malformed question (doesn't end with '?') |
| 24 | if not question.endswith("?"): |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Remove "What is" and "?" to get the expression |
| 28 | expression = question[8:-1].strip() |
| 29 | |
| 30 | # Edge Case: Empty expression after removing "What is" and "?" |
| 31 | if not expression: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | # Tokenize the expression |
| 35 | tokens = [] |
| 36 | i = 0 |
| 37 | while i < len(expression): |
| 38 | if expression[i].isspace(): |
| 39 | i += 1 |
| 40 | continue |
| 41 | elif expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()): |
| 42 | # Parse negative numbers |
| 43 | start = i |
| 44 | if expression[i] == '-': |
| 45 | i += 1 |
| 46 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 47 | i += 1 |
| 48 | # Edge Case: Invalid number format (e.g., decimal points) |
| 49 | num_str = expression[start:i] |
| 50 | if '.' in num_str: |
| 51 | raise ValueError("syntax error") |
| 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) or not expression[i:].lstrip().startswith("by"): |
| 66 | raise ValueError("syntax error") |
| 67 | # Skip "by" and add multiplication operator |
| 68 | j = i |
| 69 | while j < len(expression) and expression[j].isspace(): |
| 70 | j += 1 |
| 71 | if j+2 <= len(expression) and expression[j:j+2] == "by": |
| 72 | i = j + 2 |
| 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" and add division operator |
| 79 | j = i |
| 80 | while j < len(expression) and expression[j].isspace(): |
| 81 | j += 1 |
| 82 | if j+2 <= len(expression) and expression[j:j+2] == "by": |
| 83 | i = j + 2 |
| 84 | tokens.append("/") |
| 85 | else: |
| 86 | # Edge Case: Unsupported operations |
| 87 | raise ValueError("unknown operation") |
| 88 | |
| 89 | # Edge Case: Empty tokens list |
| 90 | if not tokens: |
| 91 | raise ValueError("syntax error") |
| 92 | |
| 93 | # Edge Case: Expression starting with an operator |
| 94 | if isinstance(tokens[0], str): |
| 95 | raise ValueError("syntax error") |
| 96 | |
| 97 | # Evaluate the expression from left to right |
| 98 | result = tokens[0] |
| 99 | i = 1 |
| 100 | |
| 101 | # Edge Case: Odd number of tokens (missing operator or operand) |
| 102 | if len(tokens) % 2 == 0: |
| 103 | raise ValueError("syntax error") |
| 104 | |
| 105 | while i < len(tokens): |
| 106 | # Edge Case: Missing operator |
| 107 | if not isinstance(tokens[i], str): |
| 108 | raise ValueError("syntax error") |
| 109 | |
| 110 | operator = tokens[i] |
| 111 | |
| 112 | # Edge Case: Missing operand |
| 113 | if i + 1 >= len(tokens) or isinstance(tokens[i + 1], str): |
| 114 | raise ValueError("syntax error") |
| 115 | |
| 116 | operand = tokens[i + 1] |
| 117 | |
| 118 | if operator == "+": |
| 119 | result += operand |
| 120 | elif operator == "-": |
| 121 | result -= operand |
| 122 | elif operator == "*": |
| 123 | result *= operand |
| 124 | elif operator == "/": |
| 125 | # Edge Case: Division by zero |
| 126 | if operand == 0: |
| 127 | raise ValueError("syntax error") |
| 128 | result //= operand # Integer division as per examples |
| 129 | else: |
| 130 | # This should not happen due to earlier validation |
| 131 | raise ValueError("unknown operation") |
| 132 | |
| 133 | i += 2 |
| 134 | |
| 135 | return result |
| 136 | # Handled Edge Cases: Empty or None input, Non-math questions, Malformed question (no ending '?'), |
| 137 | # Empty expression, Invalid number format (decimals), Unsupported operations, Empty tokens list, |
| 138 | # Expression starting with operator, Odd number of tokens, Missing operator, Missing operand, |
| 139 | # Division by zero |