| 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 | # Remove "What is" and the trailing question mark |
| 24 | expression = question[8:].rstrip('?') |
| 25 | |
| 26 | # Edge Case: Empty expression after removing "What is" and "?" |
| 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 |
| 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 | i += 1 |
| 57 | continue |
| 58 | except ValueError: |
| 59 | pass |
| 60 | |
| 61 | # Handle operations |
| 62 | if token in operations: |
| 63 | # Special handling for "multiplied by" and "divided by" |
| 64 | if token == 'multiplied' or token == 'divided': |
| 65 | # Edge Case: Incomplete "multiplied by" or "divided by" phrase |
| 66 | if i + 1 >= len(tokens): |
| 67 | raise ValueError("syntax error") |
| 68 | |
| 69 | next_token = tokens[i + 1] |
| 70 | if (token == 'multiplied' and next_token != 'by') or \ |
| 71 | (token == 'divided' and next_token != 'by'): |
| 72 | raise ValueError("syntax error") |
| 73 | |
| 74 | parsed_tokens.append(operations[token]) |
| 75 | i += 2 |
| 76 | else: |
| 77 | parsed_tokens.append(operations[token]) |
| 78 | i += 1 |
| 79 | else: |
| 80 | # Edge Case: Unknown operation |
| 81 | raise ValueError("unknown operation") |
| 82 | |
| 83 | # Edge Case: Expression starts with an operation |
| 84 | if not parsed_tokens: |
| 85 | raise ValueError("syntax error") |
| 86 | |
| 87 | if isinstance(parsed_tokens[0], str): |
| 88 | raise ValueError("syntax error") |
| 89 | |
| 90 | # Edge Case: Expression has only one number |
| 91 | if len(parsed_tokens) == 1: |
| 92 | return parsed_tokens[0] |
| 93 | |
| 94 | # Edge Case: Even number of tokens (should be odd: number, operation, number, ...) |
| 95 | if len(parsed_tokens) % 2 == 0: |
| 96 | raise ValueError("syntax error") |
| 97 | |
| 98 | # Evaluate from left to right |
| 99 | result = parsed_tokens[0] |
| 100 | |
| 101 | # Edge Case: Invalid syntax with consecutive operations or numbers |
| 102 | for i in range(1, len(parsed_tokens), 2): |
| 103 | # Check that we have operation, number pattern |
| 104 | if not isinstance(parsed_tokens[i], str) or \ |
| 105 | i + 1 >= len(parsed_tokens) or \ |
| 106 | not isinstance(parsed_tokens[i + 1], int): |
| 107 | raise ValueError("syntax error") |
| 108 | |
| 109 | operation = parsed_tokens[i] |
| 110 | operand = parsed_tokens[i + 1] |
| 111 | |
| 112 | if operation == '+': |
| 113 | result += operand |
| 114 | elif operation == '-': |
| 115 | result -= operand |
| 116 | elif operation == '*': |
| 117 | result *= operand |
| 118 | elif operation == '/': |
| 119 | # Edge Case: Division by zero |
| 120 | if operand == 0: |
| 121 | raise ValueError("syntax error") |
| 122 | result //= operand # Integer division as specified |
| 123 | else: |
| 124 | # Edge Case: Unsupported operation |
| 125 | raise ValueError("unknown operation") |
| 126 | |
| 127 | return result |
| 128 | # Handled Edge Cases: Empty input, None input, questions not starting with "What is", |
| 129 | # empty expression, no tokens, unknown operations, |
| 130 | # incomplete "multiplied by"/"divided by" phrases, |
| 131 | # expression starting with operation, single number expression, |
| 132 | # even number of tokens, consecutive operations or numbers, |
| 133 | # division by zero, unsupported operations |