Python – Concatenate Random characters in String List
Last Updated :
17 May, 2023
Given a String list, perform concatenation of random characters.
Input : test_list = [“Gfg”, “is”, “Best”, “for”, “Geeks”]
Output : “GiBfe”
Explanation : Random elements selected, e.g G from Gfg, etc.
Input : test_list = [“Gfg”, “is”, “Best”]
Output : “fst”
Explanation : Random elements selected, e.g t from Best, etc.
Method #1 : Using loop + random.choice()
In this, we extract random character using choice() and perform task of iteration using loop. The character concatenation is done using + operator.
Python3
import random
test_list = [ "Gfg" , "is" , "Best" , "for" , "Geeks" ]
print ( "The original list is : " + str (test_list))
res = ''
for ele in test_list:
res + = random.choice(ele)
print ( "Concatenated String : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'Best', 'for', 'Geeks']
Concatenated String : Gsere
Time complexity: O(n), where n is the length of the test_list. The loop + random.choice() takes O(n) time
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using list comprehension + choice() + join()
In this, we perform task of getting random using choice() and concatenation is done using join().
Python3
import random
test_list = [ "Gfg" , "is" , "Best" , "for" , "Geeks" ]
print ( "The original list is : " + str (test_list))
res = ''.join([random.choice(ele) for ele in test_list])
print ( "Concatenated String : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'Best', 'for', 'Geeks']
Concatenated String : Gitrk
Method #3 : Using recursion+random.randint()
It takes a list of strings str_list as input. It initializes an empty string result. It loops through each string s in the input list. It selects a random character from s using the random.randint() function. It concatenates the selected character to the result string. After looping through all the strings in the input list, it returns the concatenated result string.
Python3
import random
def concatenate_random_chars(str_list):
result = ''
for s in str_list:
result + = s[random.randint( 0 , len (s) - 1 )]
return result
str_list = [ 'Gfg' , 'is' , 'Best' , 'for' , 'Geeks' ]
result = concatenate_random_chars(str_list)
print ( "Concatenated String :" , result)
|
Output
Concatenated String : gitoe
Time complexity: O(n)
Auxiliary Space: O(1)
#method -4:
Approach:
n this program, we first define a sample input list of strings input_list as [‘foo’, ‘bar’, ‘baz’].
We then loop over each string in the list using the range() function and len() function. For each string, we generate a random string of lowercase letters using the random.choices() function from the random module and the string.ascii_lowercase constant. The k=5 argument specifies that we want to generate a random string of length 5.
We then concatenate the random string to the current string in the list using the += operator.
Python3
import random
import string
input_list = [ 'foo' , 'bar' , 'baz' ]
for i in range ( len (input_list)):
rand_str = ''.join(random.choices(string.ascii_lowercase, k = 5 ))
input_list[i] + = rand_str
print (input_list)
|
Output
['foofgcis', 'barluism', 'bazoyurp']
time complexity : The time complexity of the given code is O(N * K), where N is the length of the input list and K is the length of the generated random string. This is because for each element in the input list, a random string of length K is generated and then concatenated to the string, which takes O(K) time. Since this operation is performed N times, the overall time complexity is O(N * K).
space complexity: The space complexity of the code is O(N * K), as we are creating a new string of length K for each element in the input list and then concatenating it to the original string. This means that the total space required to store the updated list of strings is proportional to the product of the length of each string and the length of the randomly generated string.
Method #4: Using map() and join()
In this method, we can use the map() function to apply random.choice() on each element of the list and then use join() function to concatenate the random characters.
Steps:
- Define a lambda function that takes an element from the list and returns a random character using random.choice().
- Apply the map() function on the list using the lambda function.
- Use join() function to concatenate the random characters returned by map() function.
Python3
import random
test_list = [ "Gfg" , "is" , "Best" , "for" , "Geeks" ]
print ( "The original list is : " + str (test_list))
res = ''.join( map ( lambda ele: random.choice(ele), test_list))
print ( "Concatenated String : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'Best', 'for', 'Geeks']
Concatenated String : gisok
Time complexity: O(n * k), where n is the length of the list and k is the maximum length of an element in the list.
Auxiliary space: O(k), where k is the maximum length of an element in the list.
Approach#5: Using lambda
Generate a list of random characters from each word in the input list using the random.choice() function. Concatenate the generated characters using the join() function to form a new string.
Algorithm
1. Initialize an empty string result.
2. For each word in the input list test_list, generate a random character using the random.choice() function and append it to the result string.
3. Return the result string.
Python3
import random
test_list = [ "Gfg" , "is" , "Best" , "for" , "Geeks" ]
result = "".join( map ( lambda x: random.choice(x), test_list))
print (result)
|
Time complexity: O(n*m), n is the number of words in the input list.m is the average length of each word.Generating a random character from a word takes O(1) time.Therefore, generating a list of random characters from each word takes O(m) time. The join() function takes O(n*m) time, since it has to concatenate n lists of m characters each.
Auxiliary Space: O(n*m), The result string has a maximum length of n*m characters
Share your thoughts in the comments
Please Login to comment...