Appearance
question:def min_operations_to_transform(N: int, K: int, a: List[int], Q: int, targets: List[int]) -> List[int]: For each query, find the minimum number of operations required to make all elements of the sequence equal to the target value. Args: N (int): The number of elements in the sequence. K (int): The integer used for the XOR operation. a (List[int]): The sequence of integers. Q (int): The number of queries. targets (List[int]): The target values to which Alex wants to transform the sequence. Returns: List[int]: A list of integers representing the minimum number of operations for each query. Example: >>> min_operations_to_transform(3, 2, [1, 2, 3], 2, [3, 1]) [2, 2] >>> min_operations_to_transform(4, 3, [4, 8, 2, 6], 3, [2, 6, 0]) [3, 3, 4] pass def test_case_1(): N, K = 3, 2 a = [1, 2, 3] Q = 2 targets = [3, 1] assert min_operations_to_transform(N, K, a, Q, targets) == [2, 2] def test_case_2(): N, K = 4, 3 a = [4, 8, 2, 6] Q = 3 targets = [2, 6, 0] assert min_operations_to_transform(N, K, a, Q, targets) == [3, 3, 4] def test_case_3(): N, K = 1, 0 a = [10] Q = 1 targets = [10] assert min_operations_to_transform(N, K, a, Q, targets) == [0] def test_case_4(): N, K = 5, 5 a = [5, 5, 5, 5, 5] Q = 2 targets = [5, 0] assert min_operations_to_transform(N, K, a, Q, targets) == [0, 5] def test_case_5(): N, K = 3, 1 a = [1, 2, 3] Q = 2 targets = [3, 0] assert min_operations_to_transform(N, K, a, Q, targets) == [2, 3] def test_case_6(): N, K = 10, 4 a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Q = 1 targets = [5] assert min_operations_to_transform(N, K, a, Q, targets) == [9]
answer:def min_operations_to_transform(N, K, a, Q, targets): results = [] for X in targets: operations = 0 for value in a: if value != X: if K == 0: operations += float('inf') # Impossible to change value else: operations += 1 results.append(operations) return results
question:def long_pressed_name(name: str, typed: str) -> bool: Determine if a string `typed` could have been produced by long pressing the characters on a string `name`. >>> long_pressed_name("alex", "aaleex") True >>> long_pressed_name("saeed", "ssaaedd") False >>> long_pressed_name("leelee", "lleeelee") True >>> long_pressed_name("laiden", "laiden") True from solution import long_pressed_name def test_example_cases(): assert long_pressed_name("alex", "aaleex") == True assert long_pressed_name("saeed", "ssaaedd") == False assert long_pressed_name("leelee", "lleeelee") == True assert long_pressed_name("laiden", "laiden") == True def test_edge_cases(): assert long_pressed_name("", "") == True # Both are empty strings assert long_pressed_name("a", "a") == True # Single character, exact match assert long_pressed_name("a", "aa") == True # Single character, long pressed match assert long_pressed_name("a", "b") == False # Single character, non match def test_complex_cases(): assert long_pressed_name("alex", "aaleexa") == False # Additional unmatched character at end assert long_pressed_name("leelee", "lleeeleeee") == True # Multiple long presses assert long_pressed_name("abcdefg", "aabbccddeeffgg") == True # Each character long pressed twice assert long_pressed_name("abcdefg", "aabbccddeeffgh") == False # Last character mismatched def test_long_strings(): name = "a" * 500 + "b" * 500 typed = "a" * 1000 + "b" * 1000 assert long_pressed_name(name, typed) == True # Long strings def test_short_strings(): assert long_pressed_name("a", "aa") == True assert long_pressed_name("a", "aaa") == True
answer:def long_pressed_name(name: str, typed: str) -> bool: Check if `typed` could be produced by long pressing the characters in `name`. i, j = 0, 0 while j < len(typed): if i < len(name) and name[i] == typed[j]: i += 1 elif j == 0 or typed[j] != typed[j - 1]: return False j += 1 return i == len(name)
question:def find_odd_occurrence(arr: List[int]) -> int: Returns the integer that appears an odd number of times in the array. >>> find_odd_occurrence([1, 2, 3, 2, 3, 1, 3]) 3 >>> find_odd_occurrence([1]) 1 >>> find_odd_occurrence([4, 4, 4, 4, 4]) 4 >>> find_odd_occurrence([10, 10, 20, 20, 30, 30, 30, 30, 50]) 50 >>> find_odd_occurrence([5, 7, 9, 7, 5, 9, 8]) 8
answer:def find_odd_occurrence(arr): Returns the integer that appears an odd number of times in the array. Args: arr (list): List of integers where all integers except one appear an even number of times. Returns: int: The integer that appears an odd number of times. result = 0 for number in arr: result ^= number return result
question:def minUniqueSum(arr: List[int]) -> int: Returns the minimum possible sum of the elements of the array such that all elements are unique. You can increment elements in the array to make them unique. >>> minUniqueSum([1, 2, 2]) 6 >>> minUniqueSum([3, 2, 1, 2, 1, 7]) 22 >>> minUniqueSum([]) 0 >>> minUniqueSum([1, 2, 3]) 6 >>> minUniqueSum([1, 1, 1, 1]) 10 >>> minUniqueSum([100, 200, 100, 300]) 701
answer:def minUniqueSum(arr): Returns the minimum possible sum of the elements of the array such that all elements are unique. You can increment elements in the array to make them unique. if not arr: return 0 arr.sort() result_sum = arr[0] for i in range(1, len(arr)): if arr[i] <= arr[i-1]: arr[i] = arr[i-1] + 1 result_sum += arr[i] return result_sum