Enumeration and print error

Asked

Viewed 43 times

-1

In this list the combinations appear. But I can’t enumerate them. and when I printed the last sum total of 520. I don’t 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) 
  • for i in (10, len(lis)), could revise this line?

  • What exactly is the purpose of the program? What should it do?

  • Actually it’s for an example on the mega sena. Scroll through all 60 numbers. This example did not put the 60 numbers. Where the total of the 10 selected is 280 and 520 and each line is enumerated.

  • So basically generate all the combinations of the 60 numbers, taken 10 to 10, where the sum is between 280 and 520?

  • Indicating the amount of line that contains every generated list.

  • Not necessarily 10 to 10. Each row without being repeated from the generated list the sum of 280 in distinct positions up to the sum of 520 with different positions.

  • 2

    What you’re saying is not making any sense to me :( Is this an exercise? If it is, it would be better to post the full statement.

  • Where do these magic numbers come from 280 and 520 ??

Show 3 more comments

1 answer

1

You can use a dictionary to solve your problem.

To calculate all combinations in which the sum of the 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

Exit:

[
 (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 on Ideone.com

Browser other questions tagged

You are not signed in. Login or sign up in order to post.