0

im now starting in programing. I get one exercise to generate a combination of 10 numbers with a set of numbers, and make a sum and the sum of that numbers need to be less than 800 and higher than 700, and print the result and combination (Print All combinations).

For example if the set of numbers is 10,20,30,40,50,60,70,80,90,100 and i need to generate a set of 10 numbers using the numbers i set and the sum of that combination need to be less 560 and higher than 500.

10+20+30+40+50+60+70+80+90+100 = 550
10+20+30+40+50+40+100+80+90+90 = 550
..

I start a code in Python, but im little stuck, how i can sum the combinations.

import itertools

myList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
for i in range(len(myList)):
    for combinations in itertools.combinations(myList, i):
        print(combinations)

sum(e for e in combinations if e >= 550)
2
  • You have a duplicate 40 and one of them moved behind the 50, so I guess you really want product instead of combinations?
    – no comment
    Sep 22, 2021 at 22:43
  • Proper etiquette and responsible behavior in this and other communities such as unix.stackexchange.com (SE), where you have cross-posted, encourage you to accept perfectly good answers based on their merit. It signals to others in those communities that a good answer to your question is available and can be re-used. Please indicate the best answer for you, by checking it with a green mark to its left both here on SO and on SE.
    – Cbhihe
    Oct 4, 2021 at 8:51

2 Answers 2

0

You're very close, but you need to filter based on whether sum(e) is in the desired range, not whether e itself is.

>>> from itertools import combinations
>>> myList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
>>> [e for i in range(len(myList)) for e in combinations(myList, i) if 500 < sum(e) < 560]
[(20, 40, 50, 60, 70, 80, 90, 100), (30, 40, 50, 60, 70, 80, 90, 100), (10, 20, 30, 50, 60, 70, 80, 90, 100), (10, 20, 40, 50, 60, 70, 80, 90, 100), (10, 30, 40, 50, 60, 70, 80, 90, 100), (20, 30, 40, 50, 60, 70, 80, 90, 100)]
0

combinations only exists inside your inner for loop - you can sum it right after you print it simply with sum(combinations). Your sum() statement is outside the loop, where combinations is no longer defined.

Something like:

import itertools

myList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
for i in range(len(myList)):
    for combinations in itertools.combinations(myList, i):
        if 500 < sum(combinations) < 560:
            print(combinations, sum(combinations))
3
  • Note that even the sum of myList is 550, so your condition that it needs to be less than 560 is superfluous
    – Grismar
    Sep 22, 2021 at 22:43
  • No combination of numbers from myList (without repetition) is ever going to be greater than 550. It's like adding positive numbers together and checking that the result is greater than 0.
    – Grismar
    Sep 22, 2021 at 22:45
  • Their example contradicts the question and the code, as you already pointed out in the comments to OP - no need to have that discussion here. I added it as a comment, in case my answer is what OP is after. If it turns out they're after something else, the comment can be safely ignored.
    – Grismar
    Sep 22, 2021 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.