| 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 the mathematical operation |
| 10 | |
| 11 | Raises: |
| 12 | ValueError: If the question is malformed, contains unsupported operations, |
| 13 | or has invalid syntax |
| 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 the "What is " prefix and the trailing question mark |
| 24 | # Edge Case: Question is just "What is?" with no content |
| 25 | if question == "What is?": |
| 26 | raise ValueError("syntax error") |
| 27 | |
| 28 | content = question[8:-1].strip() |
| 29 | |
| 30 | # Edge Case: Question is "What is " with just whitespace |
| 31 | if not content: |
| 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 content |
| 43 | tokens = [] |
| 44 | i = 0 |
| 45 | while i < len(content): |
| 46 | # Skip whitespace |
| 47 | if content[i].isspace(): |
| 48 | i += 1 |
| 49 | continue |
| 50 | |
| 51 | # Check for numbers (including negative) |
| 52 | if content[i].isdigit() or (content[i] == '-' and i < len(content) - 1 and content[i+1].isdigit()): |
| 53 | start = i |
| 54 | if content[i] == '-': |
| 55 | i += 1 |
| 56 | while i < len(content) and (content[i].isdigit() or content[i] == '.'): |
| 57 | i += 1 |
| 58 | # Edge Case: Invalid number format (e.g., decimal points) |
| 59 | num_str = content[start:i] |
| 60 | if '.' in num_str: |
| 61 | raise ValueError("syntax error") |
| 62 | tokens.append(int(num_str)) |
| 63 | continue |
| 64 | |
| 65 | # Check for operations |
| 66 | found_operation = False |
| 67 | for op_text, op_symbol in operations.items(): |
| 68 | if content.startswith(op_text, i): |
| 69 | tokens.append(op_symbol) |
| 70 | i += len(op_text) |
| 71 | found_operation = True |
| 72 | break |
| 73 | |
| 74 | if found_operation: |
| 75 | continue |
| 76 | |
| 77 | # If we reach here, we have an unknown token |
| 78 | # Edge Case: Unknown operation or invalid token |
| 79 | raise ValueError("unknown operation") |
| 80 | |
| 81 | # Edge Case: No tokens found |
| 82 | if not tokens: |
| 83 | raise ValueError("syntax error") |
| 84 | |
| 85 | # Validate token sequence (should alternate number, operation, number, operation, ... ending with number) |
| 86 | # Edge Case: First token is not a number |
| 87 | if not isinstance(tokens[0], int): |
| 88 | raise ValueError("syntax error") |
| 89 | |
| 90 | # Edge Case: Even number of tokens (incomplete expression) |
| 91 | if len(tokens) % 2 == 0: |
| 92 | raise ValueError("syntax error") |
| 93 | |
| 94 | # Edge Case: Invalid token sequence (two numbers or two operations in a row) |
| 95 | for i in range(1, len(tokens)): |
| 96 | if isinstance(tokens[i], int) == isinstance(tokens[i-1], int): |
| 97 | raise ValueError("syntax error") |
| 98 | |
| 99 | # Evaluate left to right |
| 100 | result = tokens[0] |
| 101 | i = 1 |
| 102 | while i < len(tokens): |
| 103 | operation = tokens[i] |
| 104 | # Edge Case: Missing operand |
| 105 | if i + 1 >= len(tokens): |
| 106 | raise ValueError("syntax error") |
| 107 | operand = tokens[i + 1] |
| 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") # Following test expectations |
| 119 | result //= operand # Integer division as per examples |
| 120 | else: |
| 121 | # Edge Case: Unsupported operation (should have been caught earlier) |
| 122 | raise ValueError("unknown operation") |
| 123 | |
| 124 | i += 2 |
| 125 | |
| 126 | return result |
| 127 | # Handled Edge Cases: Empty input, None input, questions not starting with "What is", |
| 128 | # questions with no content after "What is", invalid number formats, |
| 129 | # unknown operations, no tokens found, first token not a number, |
| 130 | # even number of tokens, invalid token sequence, missing operands, |
| 131 | # division by zero |