Open In App

Python – K Dice Combinations

Last Updated : 01 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given the Number of Dice, get all the possible combinations.

Input : K = 2 

Output : [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), 
            (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), 
            (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), 
            (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)] 

Explanation : All possible combinations printed. 
Input : K = 1 
Output : [(1, ), (2, ), (3, ), (4, ), (5, ), (6, )] 
Explanation : All possible combinations printed.

Method #1 : Using list comprehension + product()

The combination of the above functions can be used to solve this problem. In this, we extract all the possible elements using list comprehension, and in the next step, all the possible combinations are produced using product().

Python3




# Python3 code to demonstrate working of
# K Dice Combinations
# Using list comprehension + product()
from itertools import product
 
# initializing K
K = 3
 
# using list comprehension to formulate elements
temp = [list(range(1, 7)) for _ in range(K)]
 
# using product() to get Combinations
res = list(product(*temp))
 
# printing result
print("The constructed dice Combinations : " + str(res))


Output

The constructed dice Combinations : [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 1, 6), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 4, 1), (1, 4, 2), (1, 4, 3), (1, 4, 4), (1, 4, 5), (1, 4, 6), (1, 5, 1), (1, 5, 2), (1, 5, 3), (1, 5, 4), (1, 5, 5), (1, 5, 6), (1, 6, 1), (1, 6, 2), (1, 6, 3), (1, 6, 4), (1, 6, 5), (1, 6, 6), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 1, 5), (2, 1, 6), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 2, 5), (2, 2, 6), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 4, 1), (2, 4, 2), (2, 4, 3), (2, 4, 4), (2, 4, 5), (2, 4, 6), (2, 5, 1), (2, 5, 2), (2, 5, 3), (2, 5, 4), (2, 5, 5), (2, 5, 6), (2, 6, 1), (2, 6, 2), (2, 6, 3), (2, 6, 4), (2, 6, 5), (2, 6, 6), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 1, 4), (3, 1, 5), (3, 1, 6), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 2, 4), (3, 2, 5), (3, 2, 6), (3, 3, 1), (3, 3, 2), (3, 3, 3), (3, 3, 4), (3, 3, 5), (3, 3, 6), (3, 4, 1), (3, 4, 2), (3, 4, 3), (3, 4, 4), (3, 4, 5), (3, 4, 6), (3, 5, 1), (3, 5, 2), (3, 5, 3), (3, 5, 4), (3, 5, 5), (3, 5, 6), (3, 6, 1), (3, 6, 2), (3, 6, 3), (3, 6, 4), (3, 6, 5), (3, 6, 6), (4, 1, 1), (4, 1, 2), (4, 1, 3), (4, 1, 4), (4, 1, 5), (4, 1, 6), (4, 2, 1), (4, 2, 2), (4, 2, 3), (4, 2, 4), (4, 2, 5), (4, 2, 6), (4, 3, 1), (4, 3, 2), (4, 3, 3), (4, 3, 4), (4, 3, 5), (4, 3, 6), (4, 4, 1), (4, 4, 2), (4, 4, 3), (4, 4, 4), (4, 4, 5), (4, 4, 6), (4, 5, 1), (4, 5, 2), (4, 5, 3), (4, 5, 4), (4, 5, 5), (4, 5, 6), (4, 6, 1), (4, 6, 2), (4, 6, 3), (4, 6, 4), (4, 6, 5), (4, 6, 6), (5, 1, 1), (5, 1, 2), (5, 1, 3), (5, 1, 4), (5, 1, 5), (5, 1, 6), (5, 2, 1), (5, 2, 2), (5, 2, 3), (5, 2, 4), (5, 2, 5), (5, 2, 6), (5, 3, 1), (5, 3, 2), (5, 3, 3), (5, 3, 4), (5, 3, 5), (5, 3, 6), (5, 4, 1), (5, 4, 2), (5, 4, 3), (5, 4, 4), (5, 4, 5), (5, 4, 6), (5, 5, 1), (5, 5, 2), (5, 5, 3), (5, 5, 4), (5, 5, 5), (5, 5, 6), (5, 6, 1), (5, 6, 2), (5, 6, 3), (5, 6, 4), (5, 6, 5), (5, 6, 6), (6, 1, 1), (6, 1, 2), (6, 1, 3), (6, 1, 4), (6, 1, 5), (6, 1, 6), (6, 2, 1), (6, 2, 2), (6, 2, 3), (6, 2, 4), (6, 2, 5), (6, 2, 6), (6, 3, 1), (6, 3, 2), (6, 3, 3), (6, 3, 4), (6, 3, 5), (6, 3, 6), (6, 4, 1), (6, 4, 2), (6, 4, 3), (6, 4, 4), (6, 4, 5), (6, 4, 6), (6, 5, 1), (6, 5, 2), (6, 5, 3), (6, 5, 4), (6, 5, 5), (6, 5, 6), (6, 6, 1), (6, 6, 2), (6, 6, 3), (6, 6, 4), (6, 6, 5), (6, 6, 6)]

Time complexity: O(n*n!), where n is the length of the test_list. The list comprehension + product() takes O(n*n!) time
Auxiliary Space: O(n), extra space of size n is required

Method #2 : Using repeat + product()

This is another way in which this task can be performed. In this we use repeat parameter of product to reduce one step to solve this problem, of extraction of elements.

Python3




# Python3 code to demonstrate working of
# K Dice Combinations
# Using repeat + product()
from itertools import product
 
# initializing K
K = 3
 
# using product() to get Combinations and repeat to get elements
res = list(product(range(1, 7), repeat=3))
 
# printing result
print("The constructed dice Combinations : " + str(res))


Output

The constructed dice Combinations : [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 1, 6), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 4, 1), (1, 4, 2), (1, 4, 3), (1, 4, 4), (1, 4, 5), (1, 4, 6), (1, 5, 1), (1, 5, 2), (1, 5, 3), (1, 5, 4), (1, 5, 5), (1, 5, 6), (1, 6, 1), (1, 6, 2), (1, 6, 3), (1, 6, 4), (1, 6, 5), (1, 6, 6), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 1, 5), (2, 1, 6), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 2, 5), (2, 2, 6), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 4, 1), (2, 4, 2), (2, 4, 3), (2, 4, 4), (2, 4, 5), (2, 4, 6), (2, 5, 1), (2, 5, 2), (2, 5, 3), (2, 5, 4), (2, 5, 5), (2, 5, 6), (2, 6, 1), (2, 6, 2), (2, 6, 3), (2, 6, 4), (2, 6, 5), (2, 6, 6), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 1, 4), (3, 1, 5), (3, 1, 6), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 2, 4), (3, 2, 5), (3, 2, 6), (3, 3, 1), (3, 3, 2), (3, 3, 3), (3, 3, 4), (3, 3, 5), (3, 3, 6), (3, 4, 1), (3, 4, 2), (3, 4, 3), (3, 4, 4), (3, 4, 5), (3, 4, 6), (3, 5, 1), (3, 5, 2), (3, 5, 3), (3, 5, 4), (3, 5, 5), (3, 5, 6), (3, 6, 1), (3, 6, 2), (3, 6, 3), (3, 6, 4), (3, 6, 5), (3, 6, 6), (4, 1, 1), (4, 1, 2), (4, 1, 3), (4, 1, 4), (4, 1, 5), (4, 1, 6), (4, 2, 1), (4, 2, 2), (4, 2, 3), (4, 2, 4), (4, 2, 5), (4, 2, 6), (4, 3, 1), (4, 3, 2), (4, 3, 3), (4, 3, 4), (4, 3, 5), (4, 3, 6), (4, 4, 1), (4, 4, 2), (4, 4, 3), (4, 4, 4), (4, 4, 5), (4, 4, 6), (4, 5, 1), (4, 5, 2), (4, 5, 3), (4, 5, 4), (4, 5, 5), (4, 5, 6), (4, 6, 1), (4, 6, 2), (4, 6, 3), (4, 6, 4), (4, 6, 5), (4, 6, 6), (5, 1, 1), (5, 1, 2), (5, 1, 3), (5, 1, 4), (5, 1, 5), (5, 1, 6), (5, 2, 1), (5, 2, 2), (5, 2, 3), (5, 2, 4), (5, 2, 5), (5, 2, 6), (5, 3, 1), (5, 3, 2), (5, 3, 3), (5, 3, 4), (5, 3, 5), (5, 3, 6), (5, 4, 1), (5, 4, 2), (5, 4, 3), (5, 4, 4), (5, 4, 5), (5, 4, 6), (5, 5, 1), (5, 5, 2), (5, 5, 3), (5, 5, 4), (5, 5, 5), (5, 5, 6), (5, 6, 1), (5, 6, 2), (5, 6, 3), (5, 6, 4), (5, 6, 5), (5, 6, 6), (6, 1, 1), (6, 1, 2), (6, 1, 3), (6, 1, 4), (6, 1, 5), (6, 1, 6), (6, 2, 1), (6, 2, 2), (6, 2, 3), (6, 2, 4), (6, 2, 5), (6, 2, 6), (6, 3, 1), (6, 3, 2), (6, 3, 3), (6, 3, 4), (6, 3, 5), (6, 3, 6), (6, 4, 1), (6, 4, 2), (6, 4, 3), (6, 4, 4), (6, 4, 5), (6, 4, 6), (6, 5, 1), (6, 5, 2), (6, 5, 3), (6, 5, 4), (6, 5, 5), (6, 5, 6), (6, 6, 1), (6, 6, 2), (6, 6, 3), (6, 6, 4), (6, 6, 5), (6, 6, 6)]

Method #3 : Using dynamic programming

Approach

The approach used in this code is dynamic programming, where we start with a list containing tuples of length one representing the first dice roll and then we iterate K-1 times, adding a new element to each tuple and appending the resulting tuples to a new list. Finally, we update our list to contain the new tuples and return it.

Algorithm

1. Initialize res as [(i,) for i in range(1, 7)], where i represents the possible values of a dice, i.e., 1 to 6.
2. Iterate K-1 times using a for loop and initialize new_res as an empty list.
3. For each tuple in res, iterate from 1 to 6 using another for loop and append the tuple with the new value to new_res.
4. Update res to new_res and repeat step 2 and step 3.
5. Return res.

Python3




def dice_combinations(k):
   
    res = [(i,) for i in range(1, 7)]
     
    for i in range(1, k):
        new_res = []
         
        for comb in res:
           
            for j in range(1, 7):
                new_res.append(comb + (j,))
        res = new_res
         
    return res
 
 
k = 2
 
print(dice_combinations(k))


Output

[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]

Time Complexity: O(6^K), we are generating all possible combinations of K dice rolls. The nested for loop that iterates over the range of 1 to 6 is executed K-1 times, resulting in 6^(K-1) iterations. Therefore, the total number of iterations is 6^(K-1) * 6 = 6^K.

Auxiliary Space: O(6^K), we are generating all possible combinations of K dice rolls and storing them in a list. The size of the list is 6^K, which is the total number of possible combinations of K dice rolls. However, since we are not storing any intermediate results, the space complexity can be considered as O(1) for practical purposes.

Method 4: Using recursion and memoization:

Algorithm:

  1. If k = 1, create a list with all possible rolls of one die and return it.
  2. If k > 1 and the list of all possible rolls of k-1 dice has already been computed, for each combination of rolls of k-1 dice, add all possible rolls of 3.
  3. .one die to get all possible rolls of k dice.
  4. Store the result in a memoization table to avoid recomputing already computed values.
  5. Return the list of all possible rolls of k dice.

Python3




def dice_combinations(k, memo={}):
   
    if k == 1:
        memo[1] = [(i,) for i in range(1, 7)]
        return memo[1]
       
    elif k in memo:
        return memo[k]
       
    else:
        prev_res = dice_combinations(k-1, memo)
        res = []
         
        for comb in prev_res:
            for j in range(1, 7):
                res.append(comb + (j,))
        memo[k] = res
        return res
 
 
k = 2
 
print(dice_combinations(k))
 
# This code  is contributed by Jyothi pinjala


Output

[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]

Time Complexity:
The time complexity of this algorithm can be expressed as O(6^k) because there are 6 possible rolls for each die, and the algorithm needs to calculate all the possible combinations of k dice.

Auxiliary Space:
The space complexity of this algorithm can be expressed as O(6^k) because the algorithm needs to store all the possible combinations of k dice in memory. Additionally, the memoization table can also take up to O(6^k) space.

Method 5: Using loops

Approach: 

  1. Initialize an empty list to store the combinations.
  2. Start with an empty combination (a list of K zeros).
  3. For each die, from the first to the last:
  4. For each face, from 1 to 6:
  5. Set the die value in the current combination to the current face value.
  6. If this is the last die, add the current combination to the list of combinations.
  7. Otherwise, continue with the next die by incrementing the die index.
  8. Return the list of combinations.

Python3




def dice_combinations(k):
   
    combinations = []
    current = [0] * k
     
    for i in range(k):
       
        for j in range(1, 7):
            current[i] = j
            if i == k - 1:
               
                combinations.append(tuple(current))
            else:
                i += 1
                 
    return combinations
 
 
# Example usage input
K = 3
 
combinations = dice_combinations(K)
 
print("The constructed dice combinations:", combinations)


Output

The constructed dice combinations: [(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 1, 6), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 1, 6)]

Time complexity: O(6^K), since there are 6 possible values for each of the K dice. 
Auxiliary space: O(K), since we only need to store one combination at a time.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads