Appearance
question:def can_form_query_string(T: int, test_cases: List[Tuple[int, List[str], str]]) -> List[str]: Determine if the given query string can be formed by concatenating one or more strings from the list. Args: T : int : Number of test cases test_cases : List[Tuple[int, List[str], str]] : A list of test case tuples, each containing an integer N, a list of strings, and a query string Returns: List[str] : A list of 'YES' or 'NO' for each test case Example: >>> can_form_query_string(1, [(3, ["a", "aa", "aaa"], "aaaa")]) ["YES"] >>> can_form_query_string(1, [(3, ["a", "b", "c"], "abc")]) ["YES"] >>> can_form_query_string(1, [(3, ["a", "bb", "cc"], "abcc")]) ["NO"] pass from solution import can_form_query_string def test_case_1(): T = 1 test_cases = [ (3, ["a", "aa", "aaa"], "aaaa") ] assert can_form_query_string(T, test_cases) == ["YES"] def test_case_2(): T = 1 test_cases = [ (3, ["a", "b", "c"], "abc") ] assert can_form_query_string(T, test_cases) == ["YES"] def test_case_3(): T = 1 test_cases = [ (3, ["a", "bb", "cc"], "abcc") ] assert can_form_query_string(T, test_cases) == ["NO"] def test_case_4(): T = 1 test_cases = [ (2, ["abc", "def"], "abcdef") ] assert can_form_query_string(T, test_cases) == ["YES"] def test_case_5(): T = 1 test_cases = [ (4, ["abcd", "efgh", "ijkl", "mnop"], "abcdefghijklmnop") ] assert can_form_query_string(T, test_cases) == ["YES"]
answer:def get_primes_up_to(n): primes = [] sieve = [True] * (n + 1) sieve[0] = sieve[1] = False for start in range(2, n + 1): if sieve[start]: primes.append(start) for multiple in range(start * start, n + 1, start): sieve[multiple] = False return primes def get_prime_map(): primes = get_primes_up_to(101) prime_map = {chr(97 + i): primes[i] for i in range(26)} # Mapping 'a'..'z' to prime numbers return prime_map def get_string_product(s, prime_map): product = 1 for char in s: product *= prime_map[char] return product def can_form_query_string(T, test_cases): prime_map = get_prime_map() results = [] for i in range(T): N, string_list, query_string = test_cases[i] string_products = [get_string_product(s, prime_map) for s in string_list] query_product = get_string_product(query_string, prime_map) from itertools import combinations, chain possible_products = set() possible_products.add(1) # Add product of empty sequence for length in range(1, N+1): for comb in combinations(string_products, length): combined_product = 1 for prod in comb: combined_product *= prod possible_products.add(combined_product) if query_product in possible_products: results.append("YES") else: results.append("NO") return results
question:def max_subarray_sum(arr): Finds the maximum sum subarray from the given array of integers. Parameters: arr (list): A list of integers n (1 ≤ n ≤ 10^5), where elements are in range -10^4 ≤ a[i] ≤ 10^4. Returns: tuple: A tuple containing the maximum sum and a list representing one subarray with the maximum sum. >>> max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4]) (6, [4, -1, 2, 1]) >>> max_subarray_sum([1, 2, 3, 4, 5]) (15, [1, 2, 3, 4, 5]) >>> max_subarray_sum([-1, -2, -3, -4, -5]) (-1, [-1]) >>> max_subarray_sum([5]) (5, [5]) >>> max_subarray_sum([-5]) (-5, [-5]) >>> max_subarray_sum([3, -2, 5, -1, 4]) (9, [3, -2, 5, -1, 4]) >>> large_input = list(range(-10000, 10001)) >>> max_subarray_sum(large_input)[0] 50005000 >>> max_subarray_sum([0, 0, 0, 0]) (0, [0]) >>> max_subarray_sum([-1, -2, -3, -4, -1, -2]) (-1, [-1])
answer:def max_subarray_sum(arr): Finds the maximum sum subarray from the given array of integers. Parameters: arr (list): A list of integers n (1 ≤ n ≤ 10^5), where elements are in range -10^4 ≤ a[i] ≤ 10^4. Returns: tuple: A tuple containing the maximum sum and a list representing one subarray with the maximum sum. max_sum = float('-inf') current_sum = 0 start = 0 end = 0 s = 0 for i in range(len(arr)): current_sum += arr[i] if current_sum > max_sum: max_sum = current_sum start = s end = i if current_sum < 0: current_sum = 0 s = i + 1 return (max_sum, arr[start:end+1]) # Example input and invocation # Example output should be (6, [4, -1, 2, 1]) # print(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4]))
question:class Node: def __init__(self, key=None, value=None): self.key = key self.value = value self.prev = None self.next = None class LRUCache: def __init__(self, capacity: int): Initialize the LRU cache with a given capacity. >>> cache = LRUCache(2) >>> cache.put(1, 10) >>> cache.put(2, 20) >>> cache.get(1) 10 >>> cache.put(3, 30) # evicts key 2 >>> cache.get(2) -1 >>> cache.put(4, 40) # evicts key 1 >>> cache.get(1) -1 >>> cache.get(3) 30 >>> cache.get(4) 40 self.capacity = capacity self.cache = dict() # maps key to node self.head = Node() # dummy head of the doubly linked list self.tail = Node() # dummy tail of the doubly linked list self.head.next = self.tail self.tail.prev = self.head def get(self, key: int) -> int: Retrieve the value of the key if it exists in the cache, otherwise return -1. pass def put(self, key: int, value: int) -> None: Insert or update the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least recently used item before inserting a new item. pass def test_lru_cache(): cache = LRUCache(2) cache.put(1, 10) cache.put(2, 20) assert cache.get(1) == 10 cache.put(3, 30) assert cache.get(2) == -1 cache.put(4, 40) assert cache.get(1) == -1 assert cache.get(3) == 30 assert cache.get(4) == 40 def test_lru_cache_update(): cache = LRUCache(2) cache.put(1, 1) cache.put(2, 2) cache.put(1, 10) assert cache.get(1) == 10 assert cache.get(2) == 2 def test_lru_cache_evict(): cache = LRUCache(1) cache.put(1, 1) assert cache.get(1) == 1 cache.put(2, 2) assert cache.get(1) == -1 assert cache.get(2) == 2 def test_lru_cache_capacity(): cache = LRUCache(3) cache.put(1, 1) cache.put(2, 2) cache.put(3, 3) assert cache.get(1) == 1 assert cache.get(2) == 2 assert cache.get(3) == 3 cache.put(4, 4) assert cache.get(1) == -1 assert cache.get(4) == 4 assert cache.get(3) == 3
answer:class Node: def __init__(self, key=None, value=None): self.key = key self.value = value self.prev = None self.next = None class LRUCache: def __init__(self, capacity: int): self.capacity = capacity self.cache = dict() # maps key to node self.head = Node() # dummy head of the doubly linked list self.tail = Node() # dummy tail of the doubly linked list self.head.next = self.tail self.tail.prev = self.head def _remove(self, node): prev_node = node.prev next_node = node.next prev_node.next = next_node next_node.prev = prev_node def _add(self, node): prev_tail = self.tail.prev prev_tail.next = node self.tail.prev = node node.prev = prev_tail node.next = self.tail def get(self, key: int) -> int: if key in self.cache: node = self.cache[key] self._remove(node) self._add(node) return node.value return -1 def put(self, key: int, value: int) -> None: if key in self.cache: self._remove(self.cache[key]) elif len(self.cache) >= self.capacity: lru_node = self.head.next self._remove(lru_node) del self.cache[lru_node.key] new_node = Node(key, value) self._add(new_node) self.cache[key] = new_node
question:def find_unique_triplets(nums: List[int]) -> List[List[int]]: Returns a list of unique triplets [a, b, c] in nums such that a + b + c = 0. Triplets are ordered such that a <= b <= c. >>> find_unique_triplets([-1, 0, 1, 2, -1, -4]) [[-1, 0, 1], [-1, -1, 2]] >>> find_unique_triplets([0, 1, 1]) [] >>> find_unique_triplets([1, 2, 3, 4, 5]) [] >>> find_unique_triplets([0, 0, 0, 0]) [[0, 0, 0]] >>> find_unique_triplets([-2, 0, 1, 1, 2]) [[-2, 1, 1], [-2, 0, 2]] >>> find_unique_triplets([]) [] >>> find_unique_triplets([1]) [] >>> find_unique_triplets([1, -1]) [] >>> find_unique_triplets([-1, -1, 2, 2, 0, 0, 1, 1]) [[-1, 0, 1], [-1, -1, 2]]
answer:def find_unique_triplets(nums): Returns a list of unique triplets [a, b, c] in nums such that a + b + c = 0. Triplets are ordered such that a <= b <= c. nums.sort() triplets = [] for i in range(len(nums)): if i > 0 and nums[i] == nums[i-1]: continue left, right = i+1, len(nums)-1 while left < right: total = nums[i] + nums[left] + nums[right] if total == 0: triplets.append([nums[i], nums[left], nums[right]]) # Move `left` and `right` to the next different numbers while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1 left += 1 right -= 1 elif total < 0: left += 1 else: right -= 1 return triplets