Open In App

Python | All possible N combination tuples

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python tuples, we might have a problem in which we need to generate all possible combination pairs till N. This can have application in mathematics domain. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using list comprehension + product() This task can be performed using list comprehension which can be used to iterate the container of N numbers and product() performs the task of formation of combinations from them. 

Python3




# Python3 code to demonstrate working of
# All possible N combination tuples
# Using list comprehension + product()
from itertools import product
 
# initialize N
N = 3
 
# All possible N combination tuples
# Using list comprehension + product()
res = [ele for ele in product(range(1, N + 1), repeat = N)]
 
# printing result
print("Tuple Combinations till N are : " + str(res))


Output : 

Tuple Combinations till N are : [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]

Time Complexity: O(n!) where n is the number of elements in the list “test_list”. list comprehension + product()  performs n! number of operations.
Auxiliary Space: O(n!), extra space is required where n is the number of elements in the list

  Method #2 : Using product() This task can also be performed by using just the single function. The use of list comprehension can be eliminated using the conversion to list. 

Python3




# Python3 code to demonstrate working of
# All possible N combination tuples
# Using product()
from itertools import product
 
# initialize N
N = 3
 
# All possible N combination tuples
# Using product()
res = list(product(range(1, N + 1), repeat = N))
 
# printing result
print("Tuple Combinations till N are : " + str(res))


Output : 

Tuple Combinations till N are : [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]

 Method #3 : Using tuple()

Approach

Use a recursive function to generate all possible combinations of tuples of length N. Start with an empty tuple and iterate through the elements from 1 to N. For each element, call the recursive function with the current tuple concatenated with the element.

Algorithm

1. Define a recursive function to generate all possible combinations of tuples.
2. The function takes two arguments – the current tuple and the current index.
3. If the current index is equal to N, append the current tuple to the result list and return.
4. Otherwise, iterate through the elements from 1 to N. For each element, call the recursive function with the current tuple concatenated with the element and the current index incremented by 1.
5. Initialize an empty result list and call the recursive function with an empty tuple and index 0.
6. Return the result list.

Python3




def tuple_combinations(N):
    def helper(current_tuple, index):
        if index == N:
            result.append(current_tuple)
            return
        for i in range(1, N+1):
            helper(current_tuple + (i,), index+1)
     
    result = []
    helper(tuple(), 0)
    return result
N=3
print(tuple_combinations(N))


Output

[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]

Time Complexity: O(N^N), the recursive function is called N times for each level of recursion, and there are N levels of recursion. In each call, a new tuple is created by concatenating the current tuple with an element, which takes O(N) time.
Space Complexity: O(N^N), the recursive function creates a new tuple on each call, and there are N levels of recursion. Therefore, the total number of tuples created is N^N. Additionally, the result list stores all the tuples, so its size is also N^N. The maximum size of the call stack is also N, since there are N levels of recursion.



Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads