Error in enumeration and print [closed]

-1

In this list the combinations appear. But I can not enumerate them. and when I printed the last total sum of 520. I do not know what I'm doing wrong.

from itertools import combinations 
lis = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 
39, 40, 41, 42, 43, 44, 45, 46]      
for i in (10, len(lis)):
    for comb in combinations(lis, i):
        if sum(comb) >= 280 <= 520:
            for c in enumerate(comb):
                print(comb) 
    
asked by anonymous 05.07.2018 / 03:20

1 answer

1

You can use a dictionary to solve your problem.

To calculate all combinations in which the sum of tens is between 280 and 520 :

from itertools import combinations

lis = [25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46]

dic = {}

for i in range(10, len(lis)):
    n = 0;
    for comb in combinations(lis, i):
        s = sum(comb);
        if 280 <= s <= 520:
            if s not in dic:
                dic[s] = []
            dic[s].append(comb)

print(dic[300]) # Exibe todas as combinacoes que o somatorio seja 300

Output:

[
 (25, 26, 27, 28, 29, 30, 31, 32, 33, 39),
 (25, 26, 27, 28, 29, 30, 31, 32, 34, 38),
 (25, 26, 27, 28, 29, 30, 31, 32, 35, 37),
 (25, 26, 27, 28, 29, 30, 31, 33, 34, 37),
 (25, 26, 27, 28, 29, 30, 31, 33, 35, 36),
 (25, 26, 27, 28, 29, 30, 32, 33, 34, 36),
 (25, 26, 27, 28, 29, 31, 32, 33, 34, 35)
]

See working at Ideone.com

    
05.07.2018 / 04:22