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
test_str = "geeksforgeeks"
print ( "The original string is : " + test_str)
K = 2
res = test_str[::K][:: - 1 ]
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
test_str = "geeksforgeeks"
print ( "The original string is : " + test_str)
K = 2
res = test_str[:: - 1 ][::K]
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
from functools import reduce
test_str = "geeksforgeeks"
print ( "The original string is : " + test_str)
K = 2
k = "".join( test_str[i] for i in range ( len (test_str) - 1 , - 1 , - K))
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.
Python3
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
test_str = "geeksforgeeks"
print ( "The original string is : " + test_str)
K = 2
result = reverse_interval_slice(test_str, K, 0 , "")
print ( "The reverse Interval Slice : " + result)
|
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:
Python3
reverse_interval_slicing = lambda test_str, K: test_str[::K][:: - 1 ]
test_str = "geeksforgeeks"
print ( "The original string is : " + test_str)
K = 2
res = reverse_interval_slicing(test_str, K)
print ( "The reverse Interval Slice : " + str (res))
|
Output
The original string is : geeksforgeeks
The reverse Interval Slice : segoseg
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
29 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...