Python Program to reverses alternate strings and then concatenates all elements
Last Updated :
28 Feb, 2023
Given a String List of length N, the following program returns a concatenated list of all its string elements with its alternate elements reversed.
Input: test_str = ‘geeksgeeks best for geeks’
Output: skeegskeeg best rof geeks
Explanation: Alternate words reversed.
Input: test_str = ‘geeksgeeks best geeks’
Output: skeegskeeg best skeeg
Explanation: Alternate words reversed.
Method 1: Using reversed() and loop
In this, perform the task of reversing strings using reversed() and then check for alternates using the % operator and concatenate accordingly.
Python3
test_str = 'geeksgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
temp = test_str.split()
res = []
for idx in range ( len (temp)):
if idx % 2 = = 0 :
res.append(''.join( list ( reversed (temp[idx]))))
else :
res.append(temp[idx])
res = ' ' .join(res)
print ( "Transformed String : " + str (res))
|
Output
The original string is : geeksgeeks is best for geeks
Transformed String : skeegskeeg is tseb for skeeg
Time Complexity: O(N) , a single for loop takes O(N) time complexity
Auxiliary Space: O(N)
Method 2 : Using slicing and list comprehension
In this, we perform task of reversal using slicing and then list comprehension is used to perform the task done by loop, in shorthand.
Python3
test_str = 'geeksgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
temp = test_str.split()
res = ' ' .join([''.join( list ( reversed (temp[idx]))) if idx % 2 = = 0 else temp[idx] for idx in range ( len (temp))])
print ( "Transformed String : " + str (res))
|
Output
The original string is : geeksgeeks is best for geeks
Transformed String : skeegskeeg is tseb for skeeg
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 3: Using list comprehension and enumerate
Algorithm for reversing alternate strings and then concatenating all elements using list comprehension and enumerate approach:
Input:
A string test_str
Output:
A transformed string res, which reverses alternate strings and then concatenates all elements
Steps:
Split the given string test_str into a list of words using the split() function, and assign the resulting list to a temporary variable temp.
Iterate over the list of words using enumerate() and list comprehension.
For each word, check if its index (obtained from enumerate) is even or odd. If it’s even, reverse the word using the reversed() function and join the characters to form a new string.
If the index is odd, simply keep the word as it is.
Join the resulting list of words using the join() function, with a space character as the separator.
Assign the resulting string to a variable res and return it as the final output.
Python3
test_str = 'geeksgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
temp = test_str.split()
res = ' ' .join([(''.join( list ( reversed (word))) if idx % 2 = = 0 else word) for idx, word in enumerate (temp)])
print ( "Transformed String : " + str (res))
|
Output
The original string is : geeksgeeks is best for geeks
Transformed String : skeegskeeg is tseb for skeeg
Time Complexity:
The algorithm uses a single loop and a list comprehension to iterate over the list of words and reverse alternate strings. Thus, the time complexity of the algorithm is O(n), where n is the number of words in the input string.
Auxiliary Space Complexity:
The algorithm uses two variables: temp to store the list of words and res to store the transformed string. The space complexity of these variables is O(n) since they depend on the size of the input string. However, the list comprehension uses temporary variables for each reversed string, so the space complexity is also proportional to the length of the longest string in the input. Therefore, the auxiliary space complexity of the algorithm is O(n + m), where n is the number of words in the input string and m is the length of the longest word in the input.
Method 4: Using map function and lambda function:
Algorithm:
1.Initialize the input string test_str with the value ‘geeksgeeks is best for geeks’
2.Split the input string into a list of individual words using whitespace as the delimiter and store it in the words variable.
3.Use the map function along with a lambda function to transform each element of the list.
4.If the index is even, reverse the characters of the corresponding word and return it.
5.If the index is odd, return the word as is.
6.Join the resulting list of transformed words into a string using a space as the separator and store it in the res variable.
7.Print the transformed string.
Python3
test_str = 'geeksgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
words = test_str.split()
res = ' ' .join( map ( lambda i: ''.join( list ( reversed (words[i]))) if i % 2 = = 0 else words[i], range ( len (words))))
print (res)
|
Output
The original string is : geeksgeeks is best for geeks
skeegskeeg is tseb for skeeg
Time complexity:
The time complexity of the given code is O(nm), where n is the number of words in the input string and m is the length of the longest word. This is because the code needs to iterate over each word in the input string and reverse the characters of every other word. The join function also takes O(n) time to concatenate the transformed words back into a string.
Auxiliary Space:
The space complexity of the given code is O(nm), where n is the number of words in the input string and m is the length of the longest word. This is because the code needs to store each word as a separate string in the list words, and also needs to create a new reversed string for every other word in the list. The resulting transformed string also takes O(nm) space to store.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...