Given 2 list, insert random elements from List 2 to List 1, K times at random position.
Input : test_list = [5, 7, 4, 2, 8, 1], add_list = ["Gfg", "Best", "CS"], K = 2 Output : [5, 7, 4, 2, 8, 1, 'Best', 'Gfg'] Explanation : Random elements from List 2 are added 2 times. Input : test_list = [5, 7, 4, 2, 8, 1], add_list = ["Gfg", "Best", "CS"], K = 1 Output : [5, 7, 4, 2, 8, 1, 'Gfg'] Explanation : Random elements from List 2 are added 1 times.
In this, choice() is used to get random elements from list 2 to insert into list 1 and, randint() is used to get index at which this needs to be inserted. Then list slicing is used to remake list according to newer order.
OutputThe original list : [5, 7, 4, 2, 8, 1]
The created List : ['CS', 'CS', 5, 7, 4, 'CS', 2, 8, 1]
Time complexity: O(n*n), where n is the length of the test_list. The randint() + list slicing + loop + choice() takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required
1. Define the function insert_random_elements which takes three arguments: test_list, add_list, and K.
2. Create a range object range_obj that contains the indices where new elements can be inserted. The range object should span from 0 to the length of test_list plus K.
3. Loop over K times.
4. Generate a random index rand_index using random.choice(range_obj) to randomly select an index from range_obj.
5. Insert a random element from add_list into test_list at the randomly chosen index.
6. Return the modified test_list.
Output[5, 7, 4, 'Best', 'Best', 2, 8, 1, 'Best']
Generating a random index using random.randint(0, len(test_list) - 1) takes O(1) time.
Choosing a random element from add_list using np.random.choice(add_list) takes O(1) time.
Inserting a new element into test_list at a given index using insert() takes O(n) time, where n is the length of test_list.
Therefore, the overall time complexity of the insert_random_elements() function is O(K * n), where K is the number of random elements to insert and n is the length of test_list.
The function uses O(K) additional space to store the old_element variable for each insertion.
The function also modifies the input test_list in place, so the space complexity for that is O(1).
Therefore, the overall space complexity of the insert_random_elements() function is O(K).
Create a list of random elements from add_list using the map() function and lambda function. Append the generated list to the original test_list using the + operator. Return the resultant list.
1. Initialize the test_list, add_list and K values.
2. Generate a list of K random elements from the add_list using map() function and lambda function.
3. Append the generated list to the test_list using + operator.
4. Return the resultant list.
Output[5, 7, 4, 2, 8, 1, 'Best', 'CS']
Time Complexity: O(K), as the time complexity of generating a list of K random elements using map() function and lambda function is O(K) and appending two lists using + operator takes O(n) time where n is the length of the list.
Auxiliary Space: O(K), as the space complexity of generating a list of K random elements using map() function and lambda function is O(K) and the space complexity of storing the resultant list is also O(K).