当前位置:网站首页>Use Python to guess which numbers in the set are added to get the sum of a number

Use Python to guess which numbers in the set are added to get the sum of a number

2020-11-10 10:44:25 Changchong mountain cabin

In practice, we need to guess like this

import itertools
import math
abc=22.8#10.8,22,10
list_abc=[
1.82,0.82,1.6,7.41,4.94,7.6,21.97,82.14,25.8,45.89,20.09,
16.81,0.08,0.02,1.6,7.85,6.44,0.04,63.01,0.82,9.11,1.39
]

deep=20# Control the depth of calculation 

def hwj_cha(part,all):# Unique matching difference set   Multiple identical sets are cleaned only once 
    res=all.copy()
    for x in part:
        res.remove(x)
    return tuple(res)

temp_list = []# Only filter data less than 
for dl in list_abc:
    if (dl < abc):
        temp_list.append(dl)

print(abc)
print(temp_list)
print(" Set length ="+str(len(temp_list)))

print("---------- Start calculating possibilities -----------")
list_abc_sum=round(sum(temp_list),2)
list_abc_hlen=math.floor(len(temp_list)/2)# It's only half the length 
deep=list_abc_hlen if list_abc_hlen<deep else deep# Calculate depth control parameters 
deep+=1# The loop is only executed to the range , Need to increase the 1
for i in range(0,deep):
    temp = list(itertools.combinations(temp_list, i))
    okw = False
    for j in temp:
        thesum1=round(sum(j),2)
        thesum2=round((list_abc_sum-thesum1),2)
        if (thesum1==abc or thesum2==abc):# If both guesses are correct 
            if (thesum1 == abc):
                pipei=j
                sumok=thesum1
            else:
                pipei=hwj_cha(j,temp_list);
                sumok=thesum2
            print(str(i)+"# Calculation successful :")
            print(str(sumok)+"=sum"+str(pipei))
            okw = True
            #break  # Out of contrast , If this round only makes a comparison 
    #if(okw):break  # Out of contrast , If you only make a comparison once 

  The result is as follows :

22.8
[1.82, 0.82, 1.6, 7.41, 4.94, 7.6, 21.97, 20.09, 16.81, 0.08, 0.02, 1.6, 7.85, 6.44, 0.04, 0.82, 9.11, 1.39]
 Set length =18
---------- Start calculating possibilities -----------
5# Calculation successful :
22.8=sum(0.82, 4.94, 0.08, 7.85, 9.11)
5# Calculation successful :
22.8=sum(4.94, 0.08, 7.85, 0.82, 9.11)
6# Calculation successful :
22.8=sum(1.82, 0.82, 1.6, 1.6, 7.85, 9.11)
6# Calculation successful :
22.8=sum(1.82, 1.6, 1.6, 7.85, 0.82, 9.11)
7# Calculation successful :
22.8=sum(0.82, 4.94, 0.08, 0.02, 6.44, 9.11, 1.39)
7# Calculation successful :
22.8=sum(4.94, 0.08, 0.02, 6.44, 0.82, 9.11, 1.39)
8# Calculation successful :
22.8=sum(1.82, 0.82, 1.6, 0.02, 1.6, 6.44, 9.11, 1.39)
8# Calculation successful :
22.8=sum(1.82, 1.6, 0.02, 1.6, 6.44, 0.82, 9.11, 1.39)
9# Calculation successful :
22.8=sum(1.82, 0.82, 1.6, 7.6, 0.08, 1.6, 7.85, 0.04, 1.39)
9# Calculation successful :
22.8=sum(1.82, 1.6, 7.6, 0.08, 1.6, 7.85, 0.04, 0.82, 1.39)
9# Calculation successful :
22.8=sum(1.82, 1.6, 7.6, 0.08, 1.6, 7.85, 0.04, 0.82, 1.39)
9# Calculation successful :
22.8=sum(1.82, 1.6, 7.6, 0.08, 1.6, 7.85, 0.04, 0.82, 1.39)

 

版权声明
本文为[Changchong mountain cabin]所创,转载请带上原文链接,感谢