Saturday, December 28, 2024
Google search engine
HomeLanguagesPython – Consecutive Repetition of Characters

Python – Consecutive Repetition of Characters

Sometimes, while working with character lists we can have a problem in which we need to perform consecutive repetition of characters. This can have applications in many domains. Let us discuss certain ways in which this task can be performed. 

Method #1: Using list comprehension 

This is one of the way in which this task can be performed. In this, we perform a brute force way to perform but in a one-liner for by multiplying each character by magnitude. 

Python3




# Python3 code to demonstrate working of
# Consecutive Repetition of Characters
# Using list comprehension
 
# initializing list
test_list = ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# Consecutive Repetition of Characters
# Using list comprehension
res = [sub for ele in test_list for sub in [ele] * K]
 
# printing result
print("The list after Consecutive Repetition is : " + str(res))


Output : 

The original list is : [‘g’, ‘f’, ‘g’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’] The list after Consecutive Repetition is : [‘g’, ‘g’, ‘g’, ‘f’, ‘f’, ‘f’, ‘g’, ‘g’, ‘g’, ‘i’, ‘i’, ‘i’, ‘s’, ‘s’, ‘s’, ‘b’, ‘b’, ‘b’, ‘e’, ‘e’, ‘e’, ‘s’, ‘s’, ‘s’, ‘t’, ‘t’, ‘t’]

Time Complexity: O(n) where n is the total number of values in the list “test_list”. 
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”.

Method #2: Using chain() + repeat() 

The combination of above functions can be used to solve this problem. In this, we perform the task of repeating using repeat() and the result construction using chain(). 

Python3




# Python3 code to demonstrate working of
# Consecutive Repetition of Characters
# Using chain() + repeat()
from itertools import chain, repeat
 
# initializing list
test_list = ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# Consecutive Repetition of Characters
# Using chain() + repeat()
res = list(chain.from_iterable(repeat(chr, K) for chr in test_list))
 
# printing result
print("The list after Consecutive Repetition is : " + str(res))


Output : 

The original list is : [‘g’, ‘f’, ‘g’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’] The list after Consecutive Repetition is : [‘g’, ‘g’, ‘g’, ‘f’, ‘f’, ‘f’, ‘g’, ‘g’, ‘g’, ‘i’, ‘i’, ‘i’, ‘s’, ‘s’, ‘s’, ‘b’, ‘b’, ‘b’, ‘e’, ‘e’, ‘e’, ‘s’, ‘s’, ‘s’, ‘t’, ‘t’, ‘t’]

Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the chain() + repeat() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

Method #3: Using numpy

This is the simplest method to perform this task. Numpy is a library in python that has the ability to perform array manipulations and many other things.

Python3




import numpy as np
 
# initializing list
test_list = ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# Consecutive Repetition of Characters
# Using numpy
res = np.repeat(test_list, K)
 
# printing result
print("The list after Consecutive Repetition is : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy


Output:

The original list is : [‘g’, ‘f’, ‘g’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’]
The list after Consecutive Repetition is : [‘g’ ‘g’ ‘g’ ‘f’ ‘f’ ‘f’ ‘g’ ‘g’ ‘g’ ‘i’ ‘i’ ‘i’ ‘s’ ‘s’ ‘s’ ‘b’ ‘b’ ‘b’
‘e’ ‘e’ ‘e’ ‘s’ ‘s’ ‘s’ ‘t’ ‘t’ ‘t’]
 

Time Complexity: O(n)
Auxiliary Space: O(n * K) where n is length of the original list and K is the magnitude of repetition.
Explanation:
The np.repeat function repeats the elements of the given array the number of times specified in the second argument.
In this case, we are repeating each element of the list K times, resulting in a new list containing K times repetition of each element.

Method 4 :  use a simple for loop 

Python3




# Python3 code to demonstrate working of
# Consecutive Repetition of  Characters
# Using for loop
 
# initializing list
test_list = ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# Consecutive Repetition of  Characters
# Using for loop
res = []
for chr in test_list:
    res += [chr] * K
 
# printing result
print("The list after Consecutive Repetition is : " + str(res))


Output

The original list is : ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
The list after Consecutive Repetition is : ['g', 'g', 'g', 'f', 'f', 'f', 'g', 'g', 'g', 'i', 'i', 'i', 's', 's', 's', 'b', 'b', 'b', 'e', 'e', 'e', 's', 's', 's', 't', 't', 't']

Time complexity: O(n),
Auxiliary Space: O(nK), as the output list has n*K elements.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments