Python – Swap K suffix with prefix
Last Updated :
10 May, 2023
Given a List, perform a swap of K prefix and suffix.
Examples:
Input : test_list = [5, 6, 3, 1, 0, 1, 3, 5, 7, 9], K = 2
Output : [7, 9, 3, 1, 0, 1, 3, 5, 5, 6]
Explanation : Rear 2 and Front 2 elements swapped.
Input : test_list = [5, 6, 3, 1, 0, 1, 3, 5, 7, 9], K = 1
Output : [9, 6, 3, 1, 0, 1, 3, 5, 7, 5]
Explanation : Rear 1 and Front 1 element swapped.
Method #1: Using slice and range swap
In this, we perform the task of getting the required slice using list slicing and perform range swap to swap elements. This is inplace method to solve this problem.
Python3
test_list = [ 5 , 6 , 3 , 1 , 0 , 1 , 3 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
K = 3
test_list[:K], test_list[ len (
test_list) - K:] = test_list[ len (test_list) - K:], test_list[:K]
print ( "After prefix suffix swap : " + str (test_list))
|
Output
The original list is : [5, 6, 3, 1, 0, 1, 3, 5, 7, 9]
After prefix suffix swap : [5, 7, 9, 1, 0, 1, 3, 5, 6, 3]
Time Complexity: O(1)
Auxiliary Space: O(1)
Method #2 : Using slice notation
In this we perform reconstruction of list elements using slicing each list performing slice.
Python3
test_list = [ 5 , 6 , 3 , 1 , 0 , 1 , 3 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
K = 3
res = test_list[ len (test_list) - K:] + \
test_list[K: len (test_list) - K] + test_list[:K]
print ( "After prefix suffix swap : " + str (res))
|
Output
The original list is : [5, 6, 3, 1, 0, 1, 3, 5, 7, 9]
After prefix suffix swap : [5, 7, 9, 1, 0, 1, 3, 5, 6, 3]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. sliceperforms n number of operations.
Auxiliary Space: O(1), no extra space is required
Method 3: Using a loop to swap elements
Step-by-step approach:
- Initialize two pointers, i and j, both pointing to the beginning of the list.
- Move j K steps forward.
- Swap the elements at positions i and len(test_list)-K+i.
- Increment i and decrement j until they meet at the middle of the list.
- The K suffix and prefix have now been swapped.
Python3
test_list = [ 5 , 6 , 3 , 1 , 0 , 1 , 3 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
K = 3
i = 0
j = len (test_list) - K
while j < len (test_list):
test_list[i], test_list[j] = test_list[j], test_list[i]
i + = 1
j + = 1
print ( "After prefix suffix swap : " + str (test_list))
|
Output
The original list is : [5, 6, 3, 1, 0, 1, 3, 5, 7, 9]
After prefix suffix swap : [5, 7, 9, 1, 0, 1, 3, 5, 6, 3]
Time complexity: O(K)
Auxiliary space: O(1).
Method #4: Using List Comprehension
Step-by-step approach:
- Create two slices of the original list, one for the prefix and one for the suffix.
- Reverse the suffix slice using the slice notation.
- Use list comprehension to create a new list with the swapped suffix and prefix slices, followed by the remaining middle part of the list.
- Return the final swapped list.
Python3
test_list = [ 5 , 6 , 3 , 1 , 0 , 1 , 3 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
K = 3
prefix = test_list[:K]
suffix = test_list[ - K:]
suffix = suffix[:: - 1 ]
swapped_list = [item for sublist in [suffix, test_list[K: - K], prefix] for item in sublist]
print ( "After prefix suffix swap : " + str (swapped_list))
|
Output
The original list is : [5, 6, 3, 1, 0, 1, 3, 5, 7, 9]
After prefix suffix swap : [9, 7, 5, 1, 0, 1, 3, 5, 6, 3]
Time complexity: O(n)
Auxiliary space: O(n)
Share your thoughts in the comments
Please Login to comment...