| 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 unknown 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 questions without ending question mark |
| 24 | if not question.endswith("?"): |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Remove "What is" prefix and question mark |
| 28 | expression = question[8:-1].strip() |
| 29 | |
| 30 | # Edge Case: Empty expression after removing prefix and suffix |
| 31 | if not expression: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | # Tokenize the expression |
| 35 | tokens = [] |
| 36 | i = 0 |
| 37 | |
| 38 | while i < len(expression): |
| 39 | # Skip whitespace |
| 40 | if expression[i].isspace(): |
| 41 | i += 1 |
| 42 | continue |
| 43 | |
| 44 | # Parse numbers (including negative numbers) |
| 45 | if expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()): |
| 46 | start = i |
| 47 | if expression[i] == '-': |
| 48 | i += 1 |
| 49 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '-'): |
| 50 | # Handle case where minus is actually a subtraction operator |
| 51 | if expression[i] == '-' and i > start: |
| 52 | break |
| 53 | i += 1 |
| 54 | tokens.append(int(expression[start:i])) |
| 55 | continue |
| 56 | |
| 57 | # Parse operators |
| 58 | if expression[i:].startswith("plus"): |
| 59 | tokens.append("+") |
| 60 | i += 4 |
| 61 | elif expression[i:].startswith("minus"): |
| 62 | tokens.append("-") |
| 63 | i += 5 |
| 64 | elif expression[i:].startswith("multiplied by"): |
| 65 | tokens.append("*") |
| 66 | i += 13 |
| 67 | elif expression[i:].startswith("divided by"): |
| 68 | tokens.append("/") |
| 69 | i += 10 |
| 70 | else: |
| 71 | # Edge Case: Unknown operations |
| 72 | raise ValueError("unknown operation") |
| 73 | |
| 74 | # Edge Case: Expression with no tokens |
| 75 | if not tokens: |
| 76 | raise ValueError("syntax error") |
| 77 | |
| 78 | # Edge Case: Expression starting with an operator (except minus for negative numbers) |
| 79 | if tokens[0] in ["+", "*", "/"]: |
| 80 | raise ValueError("syntax error") |
| 81 | |
| 82 | # Edge Case: Expression ending with an operator |
| 83 | if tokens[-1] in ["+", "-", "*", "/"]: |
| 84 | raise ValueError("syntax error") |
| 85 | |
| 86 | # Validate alternating pattern of number and operator |
| 87 | for i in range(len(tokens)): |
| 88 | # Even indices should be numbers |
| 89 | if i % 2 == 0: |
| 90 | if not isinstance(tokens[i], int): |
| 91 | # Edge Case: Consecutive operators |
| 92 | raise ValueError("syntax error") |
| 93 | # Odd indices should be operators |
| 94 | else: |
| 95 | if not isinstance(tokens[i], str): |
| 96 | # Edge Case: Consecutive numbers |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | # Evaluate the expression from left to right |
| 100 | result = tokens[0] |
| 101 | |
| 102 | # Edge Case: Single number expression |
| 103 | if len(tokens) == 1: |
| 104 | return result |
| 105 | |
| 106 | i = 1 |
| 107 | while i < len(tokens): |
| 108 | operator = tokens[i] |
| 109 | operand = tokens[i + 1] |
| 110 | |
| 111 | if operator == "+": |
| 112 | result += operand |
| 113 | elif operator == "-": |
| 114 | result -= operand |
| 115 | elif operator == "*": |
| 116 | result *= operand |
| 117 | elif operator == "/": |
| 118 | # Edge Case: Division by zero |
| 119 | if operand == 0: |
| 120 | raise ValueError("syntax error") |
| 121 | result //= operand # Integer division as specified |
| 122 | |
| 123 | i += 2 |
| 124 | |
| 125 | return result |
| 126 | # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark, |
| 127 | # empty expression, unknown operations, expression starting with operator, |
| 128 | # expression ending with operator, consecutive operators, consecutive numbers, |
| 129 | # single number expression, division by zero |