0 out of 464 challenges solved
Write a Python function `trim_tuple` that takes a list of tuples and an integer `k` as input. The function should return a new list of tuples where each tuple is trimmed by removing `k` elements from both the beginning and the end. If `k` is greater than or equal to half the length of a tuple, the resulting tuple should be empty. #### Example Usage ```python [main.nopy] # Example 1 input_list = [(5, 3, 2, 1, 4), (3, 4, 9, 2, 1), (9, 1, 2, 3, 5), (4, 8, 2, 1, 7)] k = 2 print(trim_tuple(input_list, k)) # Output: [(2,), (9,), (2,), (2,)] # Example 2 input_list = [(7, 8, 4, 9), (11, 8, 12, 4), (4, 1, 7, 8), (3, 6, 9, 7)] k = 1 print(trim_tuple(input_list, k)) # Output: [(8, 4), (8, 12), (1, 7), (6, 9)] ``` #### Constraints - The input list will contain tuples of integers. - The value of `k` will be a non-negative integer. - The function should handle edge cases, such as empty tuples or `k` being larger than the tuple length.
def trim_tuple(tuple_list, k): """ Trims each tuple in the list by removing k elements from both ends. Args: tuple_list (list of tuples): The list of tuples to be trimmed. k (int): The number of elements to remove from both ends. Returns: list of tuples: A new list with trimmed tuples. """ # Initialize the result list result = [] # Iterate over each tuple in the list for t in tuple_list: # Placeholder for trimming logic pass # Return the result list return result