| 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('?') |
| 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 tokens into a list of numbers and operations |
| 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 | num = int(token) |
| 59 | parsed_tokens.append(num) |
| 60 | except ValueError: |
| 61 | # Not a number, check if it's a supported operation |
| 62 | if token in operations: |
| 63 | parsed_tokens.append(operations[token]) |
| 64 | elif token == 'by': |
| 65 | # "divided by" and "multiplied by" - skip 'by' |
| 66 | # Edge Case: 'by' not preceded by 'divided' or 'multiplied' |
| 67 | if not parsed_tokens or parsed_tokens[-1] not in ['*', '/']: |
| 68 | raise ValueError("syntax error") |
| 69 | # 'by' is valid, just skip it |
| 70 | pass |
| 71 | else: |
| 72 | # Edge Case: Unknown operation |
| 73 | raise ValueError("unknown operation") |
| 74 | i += 1 |
| 75 | |
| 76 | # Edge Case: Expression ends with an operation |
| 77 | if parsed_tokens and isinstance(parsed_tokens[-1], str): |
| 78 | raise ValueError("syntax error") |
| 79 | |
| 80 | # Edge Case: Expression starts with an operation |
| 81 | if parsed_tokens and isinstance(parsed_tokens[0], str): |
| 82 | raise ValueError("syntax error") |
| 83 | |
| 84 | # Edge Case: Consecutive operations or numbers |
| 85 | for i in range(len(parsed_tokens) - 1): |
| 86 | if (isinstance(parsed_tokens[i], str) == isinstance(parsed_tokens[i+1], str)): |
| 87 | raise ValueError("syntax error") |
| 88 | |
| 89 | # Evaluate the expression from left to right |
| 90 | # Edge Case: Single number |
| 91 | if len(parsed_tokens) == 1: |
| 92 | return parsed_tokens[0] |
| 93 | |
| 94 | # Edge Case: Invalid number of tokens (should be odd: number, operation, number, ...) |
| 95 | if len(parsed_tokens) % 2 == 0: |
| 96 | raise ValueError("syntax error") |
| 97 | |
| 98 | result = parsed_tokens[0] |
| 99 | |
| 100 | # Process operations from left to right |
| 101 | for i in range(1, len(parsed_tokens), 2): |
| 102 | operation = parsed_tokens[i] |
| 103 | operand = parsed_tokens[i+1] |
| 104 | |
| 105 | # Edge Case: Missing operand |
| 106 | if i+1 >= len(parsed_tokens): |
| 107 | raise ValueError("syntax error") |
| 108 | |
| 109 | if operation == '+': |
| 110 | result += operand |
| 111 | elif operation == '-': |
| 112 | result -= operand |
| 113 | elif operation == '*': |
| 114 | result *= operand |
| 115 | elif operation == '/': |
| 116 | # Edge Case: Division by zero |
| 117 | if operand == 0: |
| 118 | raise ValueError("syntax error") |
| 119 | result //= operand |
| 120 | else: |
| 121 | # Edge Case: Unsupported operation in evaluation |
| 122 | raise ValueError("unknown operation") |
| 123 | |
| 124 | return result |
| 125 | # Handled Edge Cases: Empty input, None input, questions not starting with "What is", |
| 126 | # questions with just "What is", empty expressions, no tokens after splitting, |
| 127 | # unknown operations, invalid 'by' usage, expressions ending with operations, |
| 128 | # expressions starting with operations, consecutive operations or numbers, |
| 129 | # single number expressions, invalid token count, missing operands, division by zero, |
| 130 | # unsupported operations during evaluation |