Home > other >  All combinations with all possible lengths (recursion, backtracking)
All combinations with all possible lengths (recursion, backtracking)

Time:01-09

I have an input of type tuple. The tuple has an n-length.(e.g. (1,1,2)) I also have a list of n-elements. (e.g. [A, B, C, D]) I need to get all possible combinations of elements grouped by the tuple. (w/o creating duplicates within each sublist. Within each sublist the order does not matter ) -->

[[[A],[BC],[D]]
 [[A],[BD],[C]]
       .
       .
       .
 [[B],[A],[CD]]
 [[B],[CD],[A]]
      .
      .
      . 
 [[C],[B],[DA]]]

I believe there should be no more than 12 solutions for the example above. (takes into account that there are no duplicates and each element is used.) --> this would be a prerequisite.

Due to performance constraints, I cannot create all possibilities and then filter out the invalid ones.

`                           [ ]
                             |
  A------------ B-----------------  C ------------------D 
  |
  |
  --------- -------------
  |         |           |
  B         C           C
  C         D           D
  |         |           |
  D         B           B`

I have tried the following, however I just cannot come up with a solution myself.

def distribution (tuple_,tuple_position, base_case, solution, all_solutions, already_chosen):
    print('----------Got Called--------------')
    tuple_length = len(tuple_)
    length_of_set = tuple_[tuple_position]
    print(f'Tuple length {length_of_set} at position {tuple_position}')
    
    base_case = powersetx((i for i in base_case), length_of_set)
    print(f'#1 Base case is {base_case} the length of the base case is {len(base_case)}')
    
    for i in base_case:
        solution.append(i)
        print (f'The solution is {solution}. The length of the solution is currently {len(solution)}')
        
        base_case = powersetx_remove(base_case, [i]) #--> This list now contains only e.g. (B), (C), (D)
        print(f'#2 Base case is {base_case}')

        if len(solution) < tuple_length:
            print('Ran into else. Will go deeper.')
            tuple_position  =1
            distribution(tuple_, tuple_position, base_case, solution, all_solutions)

            #solution = []
            #tuple_position = 0
            #all_solutions.append(solution)
            #print(f'The complete solution is {all_solutions}') 
            
        
        print('Ran into else. Will go deeper.')
        tuple_position  =1
        distribution(tuple_, tuple_position, base_case, solution,all_solutions)


    print('exicted for loop')
    all_solutions.append(solution)
    print(f'All solutions are currently {all_solutions}. The length is {len(all_solutions)}.')
    solution = []
    return print('----------exited function----------'), 

distribution ((1,1,2),0,['A','B','C','D'], [],[]) `

CodePudding user response:

Your code will run as shown. There are several positional parameters to you function def, and you never specify the already_chosen parameter. Either remove it, or specify it.

Also, to go deeper that that, it would be helpful to see your definition of powersetx.

David

CodePudding user response:

Here are some codes that may not be your final solution, but may give you some ideas.

#!/usr/bin/env python

def get_list(n):
    m = n   1
    while m < length:
        print(a[n]   a[m], end=' ')
        i = 0
        while i < length:
            if i != n and i != m:
                print(a[i], end=' ')
            i  = 1
        print("")
        m  = 1
       
a = ('A', 'B', 'C', 'D')
length = len(a)
k = 0
while k < length - 1:
   get_list(k)
   k  = 1

It will produce the following output.

AB C D 
AC B D 
AD B C 
BC A D 
BD A C 
CD A B 
  • Related