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
N = 2
M = 4
res = []
for idx in range ( 1 , M):
for j in range (idx + 1 , M + 1 ):
res.append((idx, j))
print ( "The constructed combinations : " + str (res))
|
Output
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
from itertools import combinations
N = 2
M = 4
res = list (combinations( list ( range ( 1 , M + 1 )), N))
print ( "The constructed combinations : " + str (res))
|
Output
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
N = 2
M = 4
res = generate_lists(N, M)
print ( "The constructed lists : " + str (res))
|
Output
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.
Last Updated :
05 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...