| 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 | # Edge Case: Question doesn't start with "What is" |
| 20 | if not question.startswith("What is"): |
| 21 | raise ValueError("unknown operation") |
| 22 | |
| 23 | # Edge Case: Question is just "What is" with no numbers or operations |
| 24 | if question == "What is": |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Remove the "What is " prefix and the trailing question mark |
| 28 | expression = question[8:-1].strip() |
| 29 | |
| 30 | # Edge Case: Empty expression after removing prefix and question mark |
| 31 | if not expression: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | # Split the expression into tokens |
| 35 | tokens = expression.split() |
| 36 | |
| 37 | # Edge Case: No tokens after splitting |
| 38 | if not tokens: |
| 39 | raise ValueError("syntax error") |
| 40 | |
| 41 | # Define operation mappings |
| 42 | operations = { |
| 43 | "plus": "+", |
| 44 | "minus": "-", |
| 45 | "multiplied": "*", |
| 46 | "divided": "/" |
| 47 | } |
| 48 | |
| 49 | # Parse the tokens |
| 50 | parsed_tokens = [] |
| 51 | i = 0 |
| 52 | |
| 53 | while i < len(tokens): |
| 54 | token = tokens[i] |
| 55 | |
| 56 | # Try to parse as a number |
| 57 | try: |
| 58 | num = int(token) |
| 59 | parsed_tokens.append(num) |
| 60 | i += 1 |
| 61 | continue |
| 62 | except ValueError: |
| 63 | pass |
| 64 | |
| 65 | # Handle operations |
| 66 | if token in operations: |
| 67 | # Edge Case: "multiplied" and "divided" need to be followed by "by" |
| 68 | if token in ["multiplied", "divided"]: |
| 69 | if i + 1 >= len(tokens) or tokens[i + 1] != "by": |
| 70 | raise ValueError("syntax error") |
| 71 | parsed_tokens.append(operations[token]) |
| 72 | i += 2 # Skip the current token and "by" |
| 73 | else: |
| 74 | parsed_tokens.append(operations[token]) |
| 75 | i += 1 |
| 76 | else: |
| 77 | # Edge Case: Unknown operation |
| 78 | raise ValueError("unknown operation") |
| 79 | |
| 80 | # Edge Case: Expression starts with an operation |
| 81 | if not parsed_tokens: |
| 82 | raise ValueError("syntax error") |
| 83 | |
| 84 | # Edge Case: Expression starts with an operation |
| 85 | if isinstance(parsed_tokens[0], str): |
| 86 | raise ValueError("syntax error") |
| 87 | |
| 88 | # Evaluate the expression from left to right |
| 89 | result = parsed_tokens[0] |
| 90 | i = 1 |
| 91 | |
| 92 | # Edge Case: Single number with no operations |
| 93 | if len(parsed_tokens) == 1: |
| 94 | return result |
| 95 | |
| 96 | # Edge Case: Invalid syntax - odd number of tokens means missing operand |
| 97 | if len(parsed_tokens) % 2 == 0: |
| 98 | raise ValueError("syntax error") |
| 99 | |
| 100 | while i < len(parsed_tokens): |
| 101 | # Edge Case: Expected operation but found number |
| 102 | if not isinstance(parsed_tokens[i], str): |
| 103 | raise ValueError("syntax error") |
| 104 | |
| 105 | operation = parsed_tokens[i] |
| 106 | |
| 107 | # Edge Case: Expected number but found operation |
| 108 | if i + 1 >= len(parsed_tokens) or isinstance(parsed_tokens[i + 1], str): |
| 109 | raise ValueError("syntax error") |
| 110 | |
| 111 | operand = parsed_tokens[i + 1] |
| 112 | |
| 113 | if operation == "+": |
| 114 | result += operand |
| 115 | elif operation == "-": |
| 116 | result -= operand |
| 117 | elif operation == "*": |
| 118 | result *= operand |
| 119 | elif operation == "/": |
| 120 | # Edge Case: Division by zero |
| 121 | if operand == 0: |
| 122 | raise ValueError("syntax error") |
| 123 | result //= operand # Integer division as specified |
| 124 | else: |
| 125 | # Edge Case: Unsupported operation (should not happen with our parsing) |
| 126 | raise ValueError("unknown operation") |
| 127 | |
| 128 | i += 2 |
| 129 | |
| 130 | return result |
| 131 | # Handled Edge Cases: Empty input, non-math questions, missing prefix, |
| 132 | # empty expression, invalid tokens, unsupported operations, |
| 133 | # syntax errors (missing operands/operators), division by zero, |
| 134 | # invalid word combinations (multiplied without by), |
| 135 | # expressions starting with operations, single number input |