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
# Python3 code to demonstrate working of # Concatenate Random characters in String List # Using loop + choice() import random # initializing list test_list = [ "Gfg" , "is" , "Best" , "for" , "Geeks" ] # printing original list print ( "The original list is : " + str (test_list)) res = '' for ele in test_list: # Concatenating random elements res + = random.choice(ele) # printing results print ( "Concatenated String : " + str (res)) |
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
# Python3 code to demonstrate working of # Concatenate Random characters in String List # Using list comprehension + choice() + join() import random # initializing list test_list = [ "Gfg" , "is" , "Best" , "for" , "Geeks" ] # printing original list print ( "The original list is : " + str (test_list)) # characters joining using join() res = ''.join([random.choice(ele) for ele in test_list]) # printing results print ( "Concatenated String : " + str (res)) |
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) |
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 # Sample input list of strings input_list = [ 'foo' , 'bar' , 'baz' ] # Concatenate random characters to each string in the list for i in range ( len (input_list)): # Generate a random string of lowercase letters of length 5 rand_str = ''.join(random.choices(string.ascii_lowercase, k = 5 )) # Concatenate the random string to the current string in the list input_list[i] + = rand_str # Print the updated list of strings print (input_list) |
['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 # initializing list test_list = [ "Gfg" , "is" , "Best" , "for" , "Geeks" ] # printing original list print ( "The original list is : " + str (test_list)) # using map() and join() res = ''.join( map ( lambda ele: random.choice(ele), test_list)) # printing results print ( "Concatenated String : " + str (res)) |
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) |
Gstre
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