| 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 is not a valid 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 a question mark |
| 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 | |
| 42 | # Handle negative numbers |
| 43 | if expression[i] == '-' and (i == 0 or expression[i-1] in ' '): |
| 44 | i += 1 |
| 45 | if i >= len(expression) or not expression[i].isdigit(): |
| 46 | # Edge Case: Standalone minus sign |
| 47 | raise ValueError("syntax error") |
| 48 | num_start = i - 1 |
| 49 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '-'): |
| 50 | i += 1 |
| 51 | tokens.append(int(expression[num_start:i])) |
| 52 | continue |
| 53 | |
| 54 | # Handle positive numbers |
| 55 | if expression[i].isdigit(): |
| 56 | num_start = i |
| 57 | while i < len(expression) and expression[i].isdigit(): |
| 58 | i += 1 |
| 59 | tokens.append(int(expression[num_start:i])) |
| 60 | continue |
| 61 | |
| 62 | # Handle operations |
| 63 | if expression[i:].startswith("plus"): |
| 64 | tokens.append("plus") |
| 65 | i += 4 |
| 66 | elif expression[i:].startswith("minus"): |
| 67 | tokens.append("minus") |
| 68 | i += 5 |
| 69 | elif expression[i:].startswith("multiplied by"): |
| 70 | tokens.append("multiplied") |
| 71 | i += 13 |
| 72 | elif expression[i:].startswith("divided by"): |
| 73 | tokens.append("divided") |
| 74 | i += 10 |
| 75 | else: |
| 76 | # Edge Case: Unknown operations |
| 77 | raise ValueError("unknown operation") |
| 78 | |
| 79 | # Edge Case: Expression with no tokens |
| 80 | if not tokens: |
| 81 | raise ValueError("syntax error") |
| 82 | |
| 83 | # Edge Case: Expression starting with an operation |
| 84 | if isinstance(tokens[0], str): |
| 85 | raise ValueError("syntax error") |
| 86 | |
| 87 | # Evaluate the expression from left to right |
| 88 | result = tokens[0] |
| 89 | i = 1 |
| 90 | |
| 91 | while i < len(tokens): |
| 92 | # Edge Case: Consecutive operations or numbers |
| 93 | if i + 1 >= len(tokens): |
| 94 | raise ValueError("syntax error") |
| 95 | |
| 96 | # Edge Case: Expecting operation but found number |
| 97 | if not isinstance(tokens[i], str): |
| 98 | raise ValueError("syntax error") |
| 99 | |
| 100 | # Edge Case: Expecting number but found operation |
| 101 | if not isinstance(tokens[i + 1], int): |
| 102 | raise ValueError("syntax error") |
| 103 | |
| 104 | operation = tokens[i] |
| 105 | operand = tokens[i + 1] |
| 106 | |
| 107 | if operation == "plus": |
| 108 | result += operand |
| 109 | elif operation == "minus": |
| 110 | result -= operand |
| 111 | elif operation == "multiplied": |
| 112 | result *= operand |
| 113 | elif operation == "divided": |
| 114 | # Edge Case: Division by zero |
| 115 | if operand == 0: |
| 116 | raise ValueError("syntax error") |
| 117 | result //= operand # Integer division as per examples |
| 118 | else: |
| 119 | # Edge Case: Unsupported operation |
| 120 | raise ValueError("unknown operation") |
| 121 | |
| 122 | i += 2 |
| 123 | |
| 124 | return result |
| 125 | # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark, |
| 126 | # empty expression, standalone minus sign, unknown operations, |
| 127 | # expression with no tokens, expression starting with operation, |
| 128 | # consecutive operations, consecutive numbers, division by zero, |
| 129 | # expecting operation but found number, expecting number but found operation |