Open In App

Python – Swap K suffix with prefix

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Swap K suffix with prefix
# Using range swap + slice()
 
# initializing list
test_list = [5, 6, 3, 1, 0, 1, 3, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# performing range swap
test_list[:K], test_list[len(
    test_list) - K:] = test_list[len(test_list) - K:], test_list[:K]
 
# printing result
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




# Python3 code to demonstrate working of
# Swap K suffix with prefix
# Using slice notation
 
# initializing list
test_list = [5, 6, 3, 1, 0, 1, 3, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# joining parts using slice
res = test_list[len(test_list) - K:] + \
    test_list[K: len(test_list) - K] + test_list[:K]
 
# printing result
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




# Python3 code to demonstrate working of
# Swap K suffix with prefix
# Using a loop to swap elements
 
# initializing list
test_list = [5, 6, 3, 1, 0, 1, 3, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# swap suffix with prefix using loop
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
 
# printing result
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




# Python3 code to demonstrate working of
# Swap K suffix with prefix
# Using list comprehension
 
# initializing list
test_list = [5, 6, 3, 1, 0, 1, 3, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# create two slices for prefix and suffix
prefix = test_list[:K]
suffix = test_list[-K:]
 
# reverse the suffix slice
suffix = suffix[::-1]
 
# use list comprehension to create new list with swapped suffix and prefix slices, and the remaining middle part
swapped_list = [item for sublist in [suffix, test_list[K:-K], prefix] for item in sublist]
 
# printing result
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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads