Given a List, the task is to write a Python program to randomly generate N lists of size K.
Input : test_list = [6, 9, 1, 8, 4, 7], K, N = 3, 4
Output : [[8, 7, 6], [8, 6, 9], [8, 1, 6], [7, 8, 9]]
Explanation : 4 rows of 3 length are randomly extracted.
Input : test_list = [6, 9, 1, 8, 4, 7], K, N = 2, 3
Output : [[7, 6], [7, 9], [1, 9]]
Explanation : 3 rows of 2 length are randomly extracted.
In this, getting random elements is done using shuffle(), and yield with slicing is used to get K size of shuffled list.
In this, all the possible permutations of K elements are extracted using product(), and from that random sampling of N lists are done.
Time Complexity: O(n) where n is the number of elements in the list “test_list”. The product() + sample() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n), new list of size O(n) is created where n is the number of elements in the list
OutputK sized N random lists : [(9, 1, 8), (6, 8, 7), (6, 9, 4), (1, 8, 4)]
OutputThe original list is : [6, 9, 1, 8, 4, 7]
K sized N random lists : [[4, 6, 8], [1, 7, 4], [6, 9, 1], [1, 8, 7]]
OutputThe original list is : [6, 9, 1, 8, 4, 7]
K sized N random lists : [[4, 6, 8], [1, 7, 4], [6, 9, 1], [1, 8, 7]]