Python – List of N size increasing lists
Given integer N and M, construct increasing list till M, each list being N size i.e all combinations lists.
Input : N = 2, M = 3
Output : [(1, 2), (1, 3), (2, 3)]
Explanation : Increasing paired elements.Input : N = 1, M = 4
Output : [(1, ), (2, ), (3, ), (4, )]
Explanation : Increasing paired elements.
Method #1: Using loop
This is one brute force way in which this task can be performed. In this we iterate for elements till M, and form lists with N size. The drawback is that we are restricted to limited sized lists.
Step-by-step approach:
- Create an empty list called res to store the results.
- Start an outer loop that iterates from 1 to M-1 using the range() function. The loop variable is called idx.
- Inside the outer loop, start an inner loop that iterates from idx + 1 to M using the range() function. The loop variable is called j.
- Inside the inner loop, append a tuple of (idx, j) to the res list.
- After both loops have completed, print out the contents of res using the print() function and string concatenation.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of # List of N size increasing lists # Using loop # initializing N N = 2 # initializing M M = 4 # outer loop manages lists res = [] for idx in range ( 1 , M): # inner loop manages inner elements for j in range (idx + 1 , M + 1 ): res.append((idx, j)) # printing result print ( "The constructed combinations : " + str (res)) |
The constructed combinations : [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method #2 : Using combinations()
This is inbuild function offering exact functionality required for this solution. Solves the problem in one liner of getting increasing lists.
Python3
# Python3 code to demonstrate working of # List of N size increasing lists # Using combinations() from itertools import combinations # initializing N N = 2 # initializing M M = 4 # using combinations to perform task res = list (combinations( list ( range ( 1 , M + 1 )), N)) # printing result print ( "The constructed combinations : " + str (res)) |
The constructed combinations : [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
Method 3: Using Recursion.
Step-by-step approach:
- Define a recursive function that takes in three arguments: N (the number of lists to generate), M (the maximum element in each list), and start (the starting element for the first list). The function should return a list of N size-increasing lists.
- Base case: If N == 1, return a list containing all integers from start to M.
- Recursive case: For each integer, i in the range starting to M – N + 1, call the recursive function with arguments N-1, M, and i+1. This will return a list of N-1 size-increasing lists, each starting with i+1. Append the integer i to the front of each of these lists, and add all of them to a new list.
- Return the list of N size increasing lists generated in step 3.
Below is the implementation of the above approach:
Python3
def generate_lists(N, M, start = 1 ): if N = = 1 : return [[i] for i in range (start, M + 1 )] else : result = [] for i in range (start, M - N + 2 ): smaller_lists = generate_lists(N - 1 , M, i + 1 ) for smaller_list in smaller_lists: result.append([i] + smaller_list) return result # example usage N = 2 M = 4 res = generate_lists(N, M) print ( "The constructed lists : " + str (res)) |
The constructed lists : [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
Time complexity: O(M^N * N), since we are generating all possible combinations of N size-increasing lists, each with a maximum length of M, and each list requires O(N) time to generate.
Auxiliary space: O(M^N * N), since we need to store all of the generated lists in memory.
Please Login to comment...