下地スキームの生成方法について(python 3)

14267 ワード

'''
      
    6 / ,           ,  :
(1.2m, 0.8m*5, 2.3m, 4.1m, 0.65m*10, 3m*2, 1.8m, 1.9m*6, 1.5m, 5.1m*2, 3.1m*3)
        :         ,      
'''
def getBest(dict_plan): 
    #             
    # {1.2: 1, 0.8: 5, 2.3: 1} => [1.2,0.8,0.8,0.8,0.8,0.8,2.3]
    lis = [y for y in dict_plan for x in range(0, dict_plan[y])]
    lis.sort()

    res,N = [],0    #      
    while lis:
        #           1   
        if len(lis) == 1:
            res.append([lis[0]])
            break

        dir = 'Long'  #         Long     
        tmpSum = 0
        L = len(lis) - 1
        while tmpSum < 6 and lis:

            #         dir     
            tmpSum += lis[L] if dir == 'Long' else lis[0]
            if tmpSum <=6: #              
                if len(res) == N: res.append([])
                res[N].append(lis.pop(L if dir =='Long' else 0))
                L -= 1
            elif dir == 'Long' :
                #            Long       Short  
                tmpSum -= lis[L]
                dir = 'Short'
            else:
                #             Short            
                break
        N+=1
    return res

if __name__ == '__main__':

    test = {1.2: 1, 0.8: 5, 2.3: 1, 4.1: 1, 0.65: 10, 3: 2, 1.8: 1, 1.9: 6, 1.5: 1, 5.1: 2, 3.1: 3}
    res = getBest(test)
    print("    :%d"%len(res))
    for inx,item in enumerate(res):
        print("No:%2d used:%.2f" %(inx+1,sum(item)) , item)
        
11
No: 1 used:5.75 [5.1, 0.65]
No: 2 used:5.75 [5.1, 0.65]
No: 3 used:5.40 [4.1, 0.65, 0.65]
No: 4 used:5.70 [3.1, 0.65, 0.65, 0.65, 0.65]
No: 5 used:6.00 [3.1, 0.65, 0.65, 0.8, 0.8]
No: 6 used:5.50 [3.1, 0.8, 0.8, 0.8]
No: 7 used:6.00 [3, 3]
No: 8 used:5.40 [2.3, 1.9, 1.2]
No: 9 used:5.70 [1.9, 1.9, 1.9]
No:10 used:5.60 [1.9, 1.9, 1.8]
No:11 used:1.50 [1.5]