| 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 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 = expression.split() |
| 36 | |
| 37 | # Edge Case: No tokens |
| 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 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 | next_token = tokens[i + 1] |
| 62 | try: |
| 63 | num = int(next_token) |
| 64 | parsed_tokens.append(-num) |
| 65 | i += 2 |
| 66 | continue |
| 67 | except ValueError: |
| 68 | pass |
| 69 | |
| 70 | num = int(token) |
| 71 | parsed_tokens.append(num) |
| 72 | i += 1 |
| 73 | continue |
| 74 | except ValueError: |
| 75 | pass |
| 76 | |
| 77 | # Handle operations |
| 78 | if token in operations: |
| 79 | if token == "multiplied" or token == "divided": |
| 80 | # Check for "by" after "multiplied" or "divided" |
| 81 | if i + 1 >= len(tokens) or tokens[i + 1] != "by": |
| 82 | raise ValueError("syntax error") |
| 83 | parsed_tokens.append(operations[token]) |
| 84 | i += 2 |
| 85 | else: |
| 86 | parsed_tokens.append(operations[token]) |
| 87 | i += 1 |
| 88 | else: |
| 89 | # Edge Case: Unknown operations |
| 90 | raise ValueError("unknown operation") |
| 91 | |
| 92 | # Edge Case: Expression with just one number |
| 93 | if len(parsed_tokens) == 1: |
| 94 | if isinstance(parsed_tokens[0], int): |
| 95 | return parsed_tokens[0] |
| 96 | else: |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | # Edge Case: Invalid syntax - even number of tokens (should be odd: number, operator, number, ...) |
| 100 | if len(parsed_tokens) % 2 == 0: |
| 101 | raise ValueError("syntax error") |
| 102 | |
| 103 | # Edge Case: Invalid syntax - two operators in a row or missing operators |
| 104 | for i in range(len(parsed_tokens)): |
| 105 | if i % 2 == 0: # Even indices should be numbers |
| 106 | if not isinstance(parsed_tokens[i], int): |
| 107 | raise ValueError("syntax error") |
| 108 | else: # Odd indices should be operators |
| 109 | if not isinstance(parsed_tokens[i], str): |
| 110 | raise ValueError("syntax error") |
| 111 | |
| 112 | # Evaluate from left to right |
| 113 | result = parsed_tokens[0] |
| 114 | |
| 115 | # Edge Case: Division by zero |
| 116 | for i in range(1, len(parsed_tokens), 2): |
| 117 | operator = parsed_tokens[i] |
| 118 | operand = parsed_tokens[i + 1] |
| 119 | |
| 120 | if operator == "+": |
| 121 | result += operand |
| 122 | elif operator == "-": |
| 123 | result -= operand |
| 124 | elif operator == "*": |
| 125 | result *= operand |
| 126 | elif operator == "/": |
| 127 | if operand == 0: |
| 128 | raise ValueError("syntax error") # Division by zero |
| 129 | result //= operand # Integer division as per examples |
| 130 | |
| 131 | return result |
| 132 | # Handled Edge Cases: Empty input, None input, non-math questions, missing question mark, |
| 133 | # empty expression, no tokens, unknown operations, expression with just one number, |
| 134 | # invalid syntax (even number of tokens), invalid syntax (operators/numbers in wrong positions), |
| 135 | # division by zero, negative numbers, missing 'by' after 'multiplied' or 'divided' |