Open In App

Python | Reverse Interval Slicing String

Sometimes, while working with strings, we can have a problem in which we need to perform string slicing. In this, we can have a variant in which we need to perform reverse slicing that too interval. This kind of application can come in day-day programming. Let us discuss certain ways in which this task can be performed.

Method #1: Using String Slicing (1) 

This task can be performed using string slicing, that too nested one. In this, the first slice performs the Interval, and the second slice performs reverse.

Example




# Python3 code to demonstrate working of
# Reverse Interval Slicing String
# Using String Slicing 1
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing Interval
K = 2
 
# Reverse Interval Slicing String
# Using String Slicing 1
res = test_str[::K][::-1]
 
# printing result
print("The reverse Interval Slice : " + str(res))

Output
The original string is : geeksforgeeks
The reverse Interval Slice : segoseg

 
Method #2: Using String Slicing (2) 

It is another way in which this task can be performed. In this, we employ similar way as above, but a different kind of slicing.




# Python3 code to demonstrate working of
# Reverse Interval Slicing String
# Using String Slicing 2
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing Interval
K = 2
 
# Reverse Interval Slicing String
# Using String Slicing 1
res = test_str[::-1][::K]
 
# printing result
print("The reverse Interval Slice : " + str(res))

Output
The original string is : geeksforgeeks
The reverse Interval Slice : segoseg

Method#3: Using join + list comprehension 

With a specified method, we can perform this task. List comprehension is used to iterate over the string and join method is used to join the list which is formed as a product of list comprehension. 




# Python3 code to demonstrate working of
# Reverse Interval Slicing String
# Using join + list comprehension
from functools import reduce
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing Interval
K = 2
 
# Reverse Interval Slicing String
# Using join and list comprehension
k = "".join( test_str[i] for i in range(len(test_str)-1, -1, -K))
 
# printing result
print("The reverse Interval Slice : " + str(k))

Output
The original string is : geeksforgeeks
The reverse Interval Slice : segoseg

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #4: Using Recursion
Here is a recursive approach to perform the reverse interval slicing of a string.




def reverse_interval_slice(string, k, i, result):
    if i < len(string):
        result = string[i] + result
        return reverse_interval_slice(string, k, i+k, result)
    return result
  
# initializing string
test_str = "geeksforgeeks"
  
# printing original string
print("The original string is : " + test_str)
  
# initializing Interval
K = 2
  
# Reverse Interval Slicing String
# Using Recursion
result = reverse_interval_slice(test_str, K, 0, "")
  
# printing result
print("The reverse Interval Slice : " + result)
#This code is contributed by Edula Vinay Kumar Reddy

Output
The original string is : geeksforgeeks
The reverse Interval Slice : segoseg

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5: Using lambda function

In this method, the lambda function takes two arguments test_str and K, which are the string and the interval size, respectively. 

The lambda function uses string slicing to reverse the string in intervals of size K. The test_str[::K] expression gets every Kth element from the string starting from the beginning, and the [::-1] expression reverses the resulting string.

Below is the code for the above method:




reverse_interval_slicing = lambda test_str, K: test_str[::K][::-1]
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing Interval
K = 2
 
# Reverse Interval Slicing String
res = reverse_interval_slicing(test_str, K)
 
# printing result
print("The reverse Interval Slice : " + str(res))
 
# Contributed by rishabmalhdijo

Output
The original string is : geeksforgeeks
The reverse Interval Slice : segoseg

Time Complexity: O(n)
Auxiliary Space: O(n)


Article Tags :