| 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: Malformed question (doesn't end with '?' or is too short) |
| 24 | if not question.endswith('?'): |
| 25 | raise ValueError("syntax error") |
| 26 | |
| 27 | # Extract the expression part |
| 28 | expression = question[8:-1].strip() # Remove "What is " and "?" |
| 29 | |
| 30 | # Edge Case: Empty expression after removing prefix and suffix |
| 31 | if not expression: |
| 32 | raise ValueError("syntax error") |
| 33 | |
| 34 | # Define operation mappings |
| 35 | operations = { |
| 36 | "plus": "+", |
| 37 | "minus": "-", |
| 38 | "multiplied by": "*", |
| 39 | "divided by": "/" |
| 40 | } |
| 41 | |
| 42 | # Tokenize the expression |
| 43 | tokens = [] |
| 44 | i = 0 |
| 45 | while i < len(expression): |
| 46 | # Skip whitespace |
| 47 | if expression[i].isspace(): |
| 48 | i += 1 |
| 49 | continue |
| 50 | |
| 51 | # Check for numbers (including negative) |
| 52 | if expression[i].isdigit() or (expression[i] == '-' and i < len(expression) - 1 and expression[i+1].isdigit()): |
| 53 | start = i |
| 54 | if expression[i] == '-': |
| 55 | i += 1 |
| 56 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 57 | i += 1 |
| 58 | # Edge Case: Decimal numbers not allowed |
| 59 | if '.' in expression[start:i]: |
| 60 | raise ValueError("syntax error") |
| 61 | tokens.append(int(expression[start:i])) |
| 62 | continue |
| 63 | |
| 64 | # Check for operations |
| 65 | found_operation = False |
| 66 | for word_op, symbol in operations.items(): |
| 67 | if expression.startswith(word_op, i): |
| 68 | tokens.append(symbol) |
| 69 | i += len(word_op) |
| 70 | found_operation = True |
| 71 | break |
| 72 | |
| 73 | if found_operation: |
| 74 | continue |
| 75 | |
| 76 | # If we reach here, we have an unknown operation or invalid character |
| 77 | raise ValueError("unknown operation") |
| 78 | |
| 79 | # Edge Case: Expression with just a number |
| 80 | if len(tokens) == 1 and isinstance(tokens[0], int): |
| 81 | return tokens[0] |
| 82 | |
| 83 | # Edge Case: Invalid syntax - empty tokens, even number of tokens, or starts/ends with operation |
| 84 | if not tokens or len(tokens) % 2 == 0 or not isinstance(tokens[0], int) or not isinstance(tokens[-1], int): |
| 85 | raise ValueError("syntax error") |
| 86 | |
| 87 | # Validate alternating pattern of number and operation |
| 88 | for i in range(len(tokens)): |
| 89 | if i % 2 == 0: # Even indices should be numbers |
| 90 | if not isinstance(tokens[i], int): |
| 91 | raise ValueError("syntax error") |
| 92 | else: # Odd indices should be operations |
| 93 | if not isinstance(tokens[i], str): |
| 94 | raise ValueError("syntax error") |
| 95 | |
| 96 | # Evaluate left to right |
| 97 | result = tokens[0] |
| 98 | i = 1 |
| 99 | while i < len(tokens): |
| 100 | operation = tokens[i] |
| 101 | operand = tokens[i + 1] |
| 102 | |
| 103 | if operation == "+": |
| 104 | result += operand |
| 105 | elif operation == "-": |
| 106 | result -= operand |
| 107 | elif operation == "*": |
| 108 | result *= operand |
| 109 | elif operation == "/": |
| 110 | # Edge Case: Division by zero |
| 111 | if operand == 0: |
| 112 | raise ValueError("syntax error") |
| 113 | result //= operand # Integer division as per examples |
| 114 | i += 2 |
| 115 | |
| 116 | return result |
| 117 | # Handled Edge Cases: Empty input, non-math questions, missing question mark, |
| 118 | # empty expression, decimal numbers, just a number, |
| 119 | # invalid syntax patterns, division by zero |