dimanche 7 février 2021

A list in which sum of every item individually equals to 10 for example [ [1,3,3,2,1] , [4,2,2,1,1], ......]. But it doesn't work properly

Im trying to find a list front_design_array in which sum of every element equals to 10. Starting with array and f_ext_ele_groups. In each iteration, the elements in array is appended in f_ext_ele_groups. For exapmle, after first iteration, f_ext_ele_groups would turn into

[[1,1], [1,2],[1,3]...[1,9], [2,1], [2,2]......[2,8]......[3,1], [3,2]......[3,7]...............[9,1]]

At the end, front_design_array should contain, for example, [1,1,1,1,1,1,1,1,1,1] (its sum is 10) and [3,3,3,1]. After very iteration, the elements of f_ext_ele_groups is controlled for any elements whether equals to 10. If true, it is appended to front_design_array.

But front_design_array eventually just contained

[[10], [1, 1, 2, 3, 1, 2], [1, 1, 2, 3, 1, 2], [2, 1, 2, 3, 1, 1], [3, 1, 2, 3, 1], [3, 1, 2, 3, 1], [4, 1, 2, 3], [4, 1, 2, 3], [4, 1, 2, 3], [6, 1, 2, 1], [7, 1, 2], [7, 1, 2], [9, 1], [1, 1, 2, 3, 1, 2], [1, 1, 2, 3, 1, 2], [2, 1, 2, 3, 1, 1], [2, 1, 2, 3, 1, 1], [3, 1, 2, 3, 1], [5, 1, 2, 1, 1], [5, 1, 2, 1, 1], [6, 1, 2, 1], [8, 1, 1]] 

and doesnt contain for example [1,1,1,1,1,1,1,1,1,1] (its sum is 10) and [3,3,3,1] (its sum is 10).

Where is my fault?

Here is my code:

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
f_ext_ele_groups= [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]
                              
front_design_array=[]          
length=len(f_ext_ele_groups)

while length > 0:               

    draft= []
    
    for x in f_ext_ele_groups:  
                                
        sumation= sum(x)
        
        if sumation ==10:
            front_design_array.append(x)
    
        elif sumation < 10:
            
            for ele in array:
                var=x  
                sum_t=int(sumation+ ele)
  
                if sum_t <= 10:
                    var=x  
                    var.append(ele)
                    draft.append(var)
                    var=[]
                
                elif sum_t > 10:
                    continue
        
    f_ext_ele_groups=draft    
    length=len(draft)

Aucun commentaire:

Enregistrer un commentaire