| 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 valid math word problem |
| 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 math question |
| 20 | if not question.startswith("What is"): |
| 21 | raise ValueError("syntax error") |
| 22 | |
| 23 | # Remove the "What is " prefix and the trailing question mark |
| 24 | expression = question[8:].rstrip('?') |
| 25 | |
| 26 | # Edge Case: Question with just "What is" and nothing else |
| 27 | if not expression: |
| 28 | raise ValueError("syntax error") |
| 29 | |
| 30 | # Split the expression into tokens |
| 31 | tokens = expression.split() |
| 32 | |
| 33 | # Edge Case: No tokens after "What is" |
| 34 | if not tokens: |
| 35 | raise ValueError("syntax error") |
| 36 | |
| 37 | # Define operation mappings |
| 38 | operations = { |
| 39 | 'plus': '+', |
| 40 | 'minus': '-', |
| 41 | 'multiplied': '*', |
| 42 | 'divided': '/' |
| 43 | } |
| 44 | |
| 45 | # Parse tokens into a list of numbers and operations |
| 46 | parsed_tokens = [] |
| 47 | i = 0 |
| 48 | |
| 49 | while i < len(tokens): |
| 50 | token = tokens[i] |
| 51 | |
| 52 | # Try to parse as number |
| 53 | try: |
| 54 | num = int(token) |
| 55 | parsed_tokens.append(num) |
| 56 | except ValueError: |
| 57 | # Not a number, check if it's a known operation |
| 58 | if token in operations: |
| 59 | # Special handling for "multiplied by" and "divided by" |
| 60 | if token == 'multiplied' or token == 'divided': |
| 61 | # Edge Case: Incomplete "multiplied" or "divided" without "by" |
| 62 | if i + 1 >= len(tokens) or tokens[i+1] != 'by': |
| 63 | raise ValueError("syntax error") |
| 64 | parsed_tokens.append(operations[token]) |
| 65 | i += 1 # Skip the "by" token |
| 66 | else: |
| 67 | parsed_tokens.append(operations[token]) |
| 68 | # Edge Case: Unknown operation |
| 69 | elif token not in ['by']: |
| 70 | # Check if this unknown token appears in a syntactically valid position |
| 71 | # If the last parsed token was an operation, this unknown token is in operand position |
| 72 | # which should be treated as a syntax error |
| 73 | if parsed_tokens and isinstance(parsed_tokens[-1], str): |
| 74 | raise ValueError("syntax error") |
| 75 | raise ValueError("unknown operation") |
| 76 | i += 1 |
| 77 | |
| 78 | # Edge Case: Empty parsed tokens |
| 79 | if not parsed_tokens: |
| 80 | raise ValueError("syntax error") |
| 81 | |
| 82 | # Edge Case: Expression starting with an operation |
| 83 | if isinstance(parsed_tokens[0], str): |
| 84 | raise ValueError("syntax error") |
| 85 | |
| 86 | # Evaluate the expression from left to right |
| 87 | result = parsed_tokens[0] |
| 88 | i = 1 |
| 89 | |
| 90 | # Edge Case: Odd number of tokens (missing operation or operand) |
| 91 | if len(parsed_tokens) % 2 == 0: |
| 92 | raise ValueError("syntax error") |
| 93 | |
| 94 | while i < len(parsed_tokens): |
| 95 | # Edge Case: Missing operation where one is expected |
| 96 | if not isinstance(parsed_tokens[i], str): |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | operation = parsed_tokens[i] |
| 100 | |
| 101 | # Edge Case: Missing operand where one is expected |
| 102 | if i + 1 >= len(parsed_tokens) or isinstance(parsed_tokens[i+1], str): |
| 103 | raise ValueError("syntax error") |
| 104 | |
| 105 | operand = parsed_tokens[i+1] |
| 106 | |
| 107 | if operation == '+': |
| 108 | result += operand |
| 109 | elif operation == '-': |
| 110 | result -= operand |
| 111 | elif operation == '*': |
| 112 | result *= operand |
| 113 | elif operation == '/': |
| 114 | # Edge Case: Division by zero |
| 115 | if operand == 0: |
| 116 | raise ValueError("syntax error") # Following test expectations |
| 117 | result //= operand # Integer division as per examples |
| 118 | else: |
| 119 | # Edge Case: Unknown operation in evaluation phase |
| 120 | raise ValueError("unknown operation") |
| 121 | |
| 122 | i += 2 |
| 123 | |
| 124 | # Edge Case: Expression ending with an incomplete operation |
| 125 | if i > len(parsed_tokens) and i - 2 < len(parsed_tokens) - 1: |
| 126 | raise ValueError("syntax error") |
| 127 | |
| 128 | return result |
| 129 | # Handled Edge Cases: Empty input, None input, non-math questions, missing expression after "What is", |
| 130 | # unknown operations, incomplete "multiplied/divided" operations, |
| 131 | # expressions starting with operations, odd number of tokens, |
| 132 | # missing operations or operands, division by zero, |
| 133 | # unknown operations during evaluation, incomplete expressions" |