Given a String, extract K characters alternatively.
Input : test_str = ‘LazyroarLazyroarisbestforLazyroar’, K = 4
Output : geekksisforg
Explanation : Every 4th alternate range is sliced.Input : test_str = ‘LazyroarLazyroarisbest’, K = 4
Output : geekksis
Explanation : Every 4th alternate range is sliced.
Method #1 : Using loop + slicing
In this, we perform task of getting K characters using slicing, and loop is used to perform task of concatenation.
Python3
# Python3 code to demonstrate working of # Alternate K Length characters # Using loop + slicing # initializing string test_str = 'LazyroarLazyroarisbestforLazyroar' # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = 4 res = '' # skipping k * 2 for altering effect for idx in range ( 0 , len (test_str), K * 2 ): # concatenating K chars res + = test_str[idx : idx + K] # printing result print ( "Transformed String : " + str (res)) |
The original string is : LazyroarLazyroarisbestforLazyroar Transformed String : geekksisforg
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using list comprehension + join()
This is similar to the above way, only difference being its one liner approach, and join() is used to perform task of convert back to string.
Python3
# Python3 code to demonstrate working of # Alternate K Length characters # Using list comprehension + join() # initializing string test_str = 'LazyroarLazyroarisbestforLazyroar' # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = 4 # slicing K using slicing, join for converting back to string res = ''.join([test_str[idx : idx + K] for idx in range ( 0 , len (test_str), K * 2 )]) # printing result print ( "Transformed String : " + str (res)) |
The original string is : LazyroarLazyroarisbestforLazyroar Transformed String : geekksisforg
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using map and lambda function.
Python3
# Python3 code to demonstrate working of # Alternate K Length characters # Using map and lambda function: # initializing string test_str = 'LazyroarLazyroarisbestforLazyroar' # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = 4 result = ''.join( map ( lambda x: test_str[x:x + K], range ( 0 , len (test_str), 2 * K))) # printing result print ( "Transformed String : " + str (result)) #this code contributed by tvsk. |
The original string is : LazyroarLazyroarisbestforLazyroar Transformed String : geekksisforg
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Here’s an implementation using the reduce function from the functools module:
The reduce function applies the lambda function to the elements of the list and accumulates the results. In this case, the lambda function takes two arguments x and y, which are the previous and current elements in the list respectively, and concatenates them. The reduce function starts with the first two elements of the list and the result is the final concatenated string.
Python3
from functools import reduce # initializing string test_str = 'LazyroarLazyroarisbestforLazyroar' # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = 4 # using reduce to concatenate the K characters result = reduce ( lambda x, y: x + y, [test_str[i:i + K] for i in range ( 0 , len (test_str), 2 * K)]) # printing result print ( "Transformed String : " + str (result)) |
The original string is : LazyroarLazyroarisbestforLazyroar Transformed String : geekksisforg
The time and auxiliary space for this implementation will also be O(n).
Method 5 : using a generator function
step-by-step approach
- Define a function named chunk_generator that takes two arguments: a string s and an integer k.
- The function uses a for loop with a range of 0 to the length of the input string, with a step of k*2. This skips 2*k characters each time to alternate between the chunks.
- The loop yields a slice of the string, starting from index i and going up to index i+k. This slice contains a chunk of k characters from the input string.
- In the main program, initialize a string test_str and an integer K.
- Call the chunk_generator function with the test_str and K arguments. This generates a generator object that yields chunks of K characters.
- Use the join method to concatenate the chunks into a single string, and assign the result to a variable named res.
- Print the resulting string, with the message “Transformed String : ” concatenated to the beginning of the string.
Python3
# defining chunk generator function def chunk_generator(s, k): for i in range ( 0 , len (s), k * 2 ): yield s[i:i + k] # initializing string and K test_str = 'LazyroarLazyroarisbestforLazyroar' K = 4 # generating chunks and joining them together res = ''.join(chunk_generator(test_str, K)) # printing result print ( "Transformed String : " + str (res)) |
Transformed String : geekksisforg
Time complexity: The program has a time complexity of O(n/k), where n is the length of the input string and k is the length of each chunk.
Auxiliary space complexity: The program has an auxiliary space complexity of O(k), which is the size of each chunk.