| 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: 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 question mark |
| 28 | expression = question[8:].rstrip('?').strip() |
| 29 | |
| 30 | # Edge Case: Empty expression after removing prefix and suffix |
| 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 tokens into a list of numbers and operators |
| 50 | parsed_tokens = [] |
| 51 | i = 0 |
| 52 | |
| 53 | while i < len(tokens): |
| 54 | token = tokens[i] |
| 55 | |
| 56 | # Try to parse as number |
| 57 | try: |
| 58 | # Handle negative numbers |
| 59 | if token == '-' and i + 1 < len(tokens): |
| 60 | # Check if next token is a number |
| 61 | try: |
| 62 | num = int(tokens[i + 1]) |
| 63 | parsed_tokens.append(-num) |
| 64 | i += 2 |
| 65 | continue |
| 66 | except ValueError: |
| 67 | pass |
| 68 | |
| 69 | parsed_tokens.append(int(token)) |
| 70 | i += 1 |
| 71 | continue |
| 72 | except ValueError: |
| 73 | pass |
| 74 | |
| 75 | # Handle operations |
| 76 | if token in operations: |
| 77 | if token == 'multiplied' or token == 'divided': |
| 78 | # These operations require 'by' after them |
| 79 | # Edge Case: Missing 'by' after 'multiplied' or 'divided' |
| 80 | if i + 1 >= len(tokens) or tokens[i + 1] != 'by': |
| 81 | raise ValueError("syntax error") |
| 82 | i += 1 # Skip 'by' |
| 83 | |
| 84 | parsed_tokens.append(operations[token]) |
| 85 | i += 1 |
| 86 | continue |
| 87 | |
| 88 | # Handle special cases |
| 89 | if token == 'by': |
| 90 | # Edge Case: 'by' in wrong position |
| 91 | raise ValueError("syntax error") |
| 92 | |
| 93 | # Edge Case: Unknown operation |
| 94 | raise ValueError("unknown operation") |
| 95 | |
| 96 | # Edge Case: Expression starts with an operator |
| 97 | if not parsed_tokens: |
| 98 | raise ValueError("syntax error") |
| 99 | |
| 100 | if isinstance(parsed_tokens[0], str): |
| 101 | raise ValueError("syntax error") |
| 102 | |
| 103 | # Evaluate the expression from left to right |
| 104 | result = parsed_tokens[0] |
| 105 | i = 1 |
| 106 | |
| 107 | # Edge Case: Single number with no operations |
| 108 | if len(parsed_tokens) == 1: |
| 109 | return result |
| 110 | |
| 111 | # Edge Case: Odd number of tokens (should be even after first number) |
| 112 | if len(parsed_tokens) % 2 == 0: |
| 113 | raise ValueError("syntax error") |
| 114 | |
| 115 | while i < len(parsed_tokens): |
| 116 | # Edge Case: Missing operator |
| 117 | if i + 1 >= len(parsed_tokens): |
| 118 | raise ValueError("syntax error") |
| 119 | |
| 120 | operator = parsed_tokens[i] |
| 121 | operand = parsed_tokens[i + 1] |
| 122 | |
| 123 | # Edge Case: Operator is not a string or operand is not a number |
| 124 | if not isinstance(operator, str) or not isinstance(operand, int): |
| 125 | raise ValueError("syntax error") |
| 126 | |
| 127 | # Edge Case: Division by zero |
| 128 | if operator == '/' and operand == 0: |
| 129 | raise ValueError("syntax error") |
| 130 | |
| 131 | # Perform the operation |
| 132 | if operator == '+': |
| 133 | result += operand |
| 134 | elif operator == '-': |
| 135 | result -= operand |
| 136 | elif operator == '*': |
| 137 | result *= operand |
| 138 | elif operator == '/': |
| 139 | result //= operand # Integer division as specified |
| 140 | else: |
| 141 | # Edge Case: Unsupported operation |
| 142 | raise ValueError("unknown operation") |
| 143 | |
| 144 | i += 2 |
| 145 | |
| 146 | return result |
| 147 | # Handled Edge Cases: Empty input, invalid question format, missing numbers/operations, |
| 148 | # unknown operations, syntax errors, division by zero, negative numbers, |
| 149 | # missing 'by' after multiplied/divided, incorrect token ordering |