| 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 unsupported operations, |
| 13 | or has invalid syntax |
| 14 | """ |
| 15 | # Edge Case: Empty or None input |
| 16 | if not question: |
| 17 | raise ValueError("syntax error") |
| 18 | |
| 19 | # Check if it's a valid question format |
| 20 | if not question.startswith("What is") or not question.endswith("?"): |
| 21 | raise ValueError("unknown operation") |
| 22 | |
| 23 | # Extract the expression part |
| 24 | expression = question[8:-1].strip() |
| 25 | |
| 26 | # Edge Case: Empty expression after "What is" and before "?" |
| 27 | if not expression: |
| 28 | raise ValueError("syntax error") |
| 29 | |
| 30 | # Split the expression into tokens |
| 31 | tokens = expression.split() |
| 32 | |
| 33 | # Edge Case: Only a number with no operations |
| 34 | if len(tokens) == 1: |
| 35 | try: |
| 36 | return int(tokens[0]) |
| 37 | except ValueError: |
| 38 | raise ValueError("syntax error") |
| 39 | |
| 40 | # Parse the tokens |
| 41 | equation = [] |
| 42 | i = 0 |
| 43 | |
| 44 | # Edge Case: Malformed expression starting with an operator |
| 45 | if tokens[0] in ["plus", "minus", "multiplied", "divided"]: |
| 46 | raise ValueError("syntax error") |
| 47 | |
| 48 | while i < len(tokens): |
| 49 | token = tokens[i] |
| 50 | |
| 51 | # Handle numbers |
| 52 | if token.lstrip('-').isdigit(): |
| 53 | equation.append(int(token)) |
| 54 | # Handle operators |
| 55 | elif token == "plus": |
| 56 | equation.append("+") |
| 57 | elif token == "minus": |
| 58 | equation.append("-") |
| 59 | elif token == "multiplied" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 60 | equation.append("*") |
| 61 | i += 1 # Skip "by" |
| 62 | elif token == "divided" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 63 | equation.append("/") |
| 64 | i += 1 # Skip "by" |
| 65 | else: |
| 66 | # Edge Case: Unknown operation |
| 67 | raise ValueError("unknown operation") |
| 68 | |
| 69 | i += 1 |
| 70 | |
| 71 | # Edge Case: Expression ending with an operator |
| 72 | if len(equation) % 2 == 0: |
| 73 | raise ValueError("syntax error") |
| 74 | |
| 75 | # Edge Case: Invalid sequence (operator followed by operator) |
| 76 | for j in range(len(equation)): |
| 77 | if j % 2 == 1: # Operators should be at odd indices |
| 78 | if not isinstance(equation[j], str): |
| 79 | raise ValueError("syntax error") |
| 80 | else: # Numbers should be at even indices |
| 81 | if not isinstance(equation[j], int): |
| 82 | raise ValueError("syntax error") |
| 83 | |
| 84 | # Evaluate the expression from left to right |
| 85 | result = equation[0] |
| 86 | |
| 87 | # Edge Case: Malformed expression with insufficient tokens |
| 88 | if len(equation) < 3: |
| 89 | raise ValueError("syntax error") |
| 90 | |
| 91 | i = 1 |
| 92 | while i < len(equation): |
| 93 | operator = equation[i] |
| 94 | # Edge Case: Missing operand |
| 95 | if i + 1 >= len(equation): |
| 96 | raise ValueError("syntax error") |
| 97 | |
| 98 | operand = equation[i + 1] |
| 99 | |
| 100 | if operator == "+": |
| 101 | result += operand |
| 102 | elif operator == "-": |
| 103 | result -= operand |
| 104 | elif operator == "*": |
| 105 | result *= operand |
| 106 | elif operator == "/": |
| 107 | # Edge Case: Division by zero |
| 108 | if operand == 0: |
| 109 | raise ValueError("syntax error") |
| 110 | result //= operand # Integer division as per examples |
| 111 | else: |
| 112 | # Edge Case: Unknown operator in evaluation |
| 113 | raise ValueError("unknown operation") |
| 114 | |
| 115 | i += 2 |
| 116 | |
| 117 | return result |
| 118 | # Handled Edge Cases: Empty input, invalid question format, empty expression, |
| 119 | # only a number, expression starting with operator, |
| 120 | # unknown operations, expression ending with operator, |
| 121 | # invalid sequences, malformed expressions, missing operands, |
| 122 | # division by zero, unknown operators during evaluation |