0 out of 464 challenges solved
Write a function `remove_nested` that takes a tuple as input and returns a new tuple with all nested tuples removed. The function should iterate through the elements of the input tuple and include only those elements that are not tuples themselves. #### Example Usage ```python [main.nopy] print(remove_nested((1, 5, 7, (4, 6), 10))) # Output: (1, 5, 7, 10) print(remove_nested((2, 6, 8, (5, 7), 11))) # Output: (2, 6, 8, 11) print(remove_nested((3, 7, 9, (6, 8), 12))) # Output: (3, 7, 9, 12) print(remove_nested((3, 7, 9, (6, 8), (5, 12), 12))) # Output: (3, 7, 9, 12) ``` #### Constraints - The input will always be a tuple. - The elements of the tuple can be of any type. - Nested tuples can appear at any position in the input tuple.
def remove_nested(input_tuple): """ Remove all nested tuples from the given tuple. Args: input_tuple (tuple): The input tuple containing elements. Returns: tuple: A new tuple with all nested tuples removed. """ # Initialize an empty tuple to store the result result = () # Iterate through each element in the input tuple for element in input_tuple: # Check if the element is not a tuple if not isinstance(element, tuple): # Add the element to the result tuple result += (element,) # Return the result tuple return result