| 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 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 question without ending 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 | # 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 | |
| 46 | # Edge Case: Handle negative numbers |
| 47 | # If expression starts with minus, it's a negative number |
| 48 | while i < len(expression): |
| 49 | # Skip whitespace |
| 50 | if expression[i].isspace(): |
| 51 | i += 1 |
| 52 | continue |
| 53 | |
| 54 | # Check for numbers (including negative numbers) |
| 55 | if expression[i].isdigit() or (expression[i] == '-' and (i == 0 or expression[i-1].isspace())): |
| 56 | start = i |
| 57 | if expression[i] == '-': |
| 58 | i += 1 |
| 59 | while i < len(expression) and (expression[i].isdigit() or expression[i] == '.'): |
| 60 | i += 1 |
| 61 | # Edge Case: Non-integer numbers |
| 62 | num_str = expression[start:i] |
| 63 | if '.' in num_str: |
| 64 | raise ValueError("syntax error") |
| 65 | try: |
| 66 | tokens.append(int(num_str)) |
| 67 | except ValueError: |
| 68 | raise ValueError("syntax error") |
| 69 | continue |
| 70 | |
| 71 | # Check for operations |
| 72 | found_operation = False |
| 73 | for word_op, symbol in operations.items(): |
| 74 | if expression.startswith(word_op, i): |
| 75 | tokens.append(symbol) |
| 76 | i += len(word_op) |
| 77 | found_operation = True |
| 78 | break |
| 79 | |
| 80 | if found_operation: |
| 81 | continue |
| 82 | |
| 83 | # If we reach here, we have an unknown operation or invalid character |
| 84 | raise ValueError("unknown operation") |
| 85 | |
| 86 | # Edge Case: Empty tokens list |
| 87 | if not tokens: |
| 88 | raise ValueError("syntax error") |
| 89 | |
| 90 | # Edge Case: Single number |
| 91 | if len(tokens) == 1: |
| 92 | if isinstance(tokens[0], int): |
| 93 | return tokens[0] |
| 94 | else: |
| 95 | raise ValueError("syntax error") |
| 96 | |
| 97 | # Edge Case: Invalid syntax - operations without numbers or vice versa |
| 98 | for i in range(len(tokens)): |
| 99 | if i % 2 == 0: # Even indices should be numbers |
| 100 | if not isinstance(tokens[i], int): |
| 101 | raise ValueError("syntax error") |
| 102 | else: # Odd indices should be operations |
| 103 | if not isinstance(tokens[i], str): |
| 104 | raise ValueError("syntax error") |
| 105 | |
| 106 | # Edge Case: Even number of tokens (should be odd: number-operation-number...) |
| 107 | if len(tokens) % 2 == 0: |
| 108 | raise ValueError("syntax error") |
| 109 | |
| 110 | # Evaluate from left to right |
| 111 | result = tokens[0] |
| 112 | |
| 113 | # Edge Case: Division by zero |
| 114 | for i in range(1, len(tokens), 2): |
| 115 | operation = tokens[i] |
| 116 | operand = tokens[i + 1] |
| 117 | |
| 118 | if operation == "+": |
| 119 | result += operand |
| 120 | elif operation == "-": |
| 121 | result -= operand |
| 122 | elif operation == "*": |
| 123 | result *= operand |
| 124 | elif operation == "/": |
| 125 | if operand == 0: |
| 126 | raise ValueError("syntax error") # Division by zero |
| 127 | result //= operand # Integer division as per examples |
| 128 | |
| 129 | return result |
| 130 | # Handled Edge Cases: Empty/None input, non-math questions, missing question mark, |
| 131 | # empty expression, negative numbers, non-integer numbers, |
| 132 | # unknown operations, empty tokens, single number, invalid syntax, |
| 133 | # even number of tokens, division by zero |