How to Create a Symmetric Array in Python: A Fun Logic Exercise
Here's a Python program that says to write a Symmetric array transformation. A top interview question. Symmetric Array Transformation Problem: Write a Python function that transforms a given array into a symmetric array by mirroring it around its center. For example: Input: [1, 2, 3] Output: [1, 2, 3, 2, 1] Hints: Use slicing for the reverse part. Concatenate the original array with its mirrored part. Example def symmetric_array(arr): """ Transforms the input array into a symmetric array by mirroring it around its center. Parameters: arr (list): The input array. Returns: list: The symmetric array. """ # Mirror the array by concatenating the original with its reverse (excluding the last element to avoid duplication) return arr + arr[-2::-1] # Example usage input_array = [1, 2, 3] symmetric_result = symmetric_array(input_array) print("Input Array:", input_arr...
Comments
Post a Comment
Thanks for your message. We will get back you.