lets say i have a list:
L = [1,2,3,4,5,6,7,8,9]

x could be anything, but it wont ever be longer than len(L) - 1.. just for what im asking, im setting it to 3
x = 3

value could also be anything, but not exceeding sum(L)
value = 8


What i am trying to figure out is what combinations of items in the list are equal to value, but the length of the combonation cannot exceed x and you cannot use the same item twice.

the anwsers i would want returned are (but not limited to):
[8], [1,7], [2,6], [3,5], [4,4], [1,2,5], [1,3,4], [1,4,3]

You wouldn't return combination of 4 list items because that would exceed x.

Just fyi, this is not homework lol. This is a personal project im working on, and im just stumped.

I wish i had some broken code to post, but i can't even get that far... im not sure how to tackle this since the value of x can be different... if i had to find a static number of list items whos value = 8, i could do that easily... but im lost with this.

My best attempt used L[a:b] changing the values of b and a as b - a approached x and y <= len(L) ... it gave me results, but only if the list items were next to eachother. (8 would not work, but 9 will give you some returns)


Thanks alot for reading the wall of text, and I apreciate any and all anwsers!

Recommended Answers

All 7 Replies

just something i forgot... the length of L is also subject to change.

The question is not clear, so let us say
if number_list = [1,2,3,4,5,6,7,8,9]
3 = maximum numbers that can be combined
(and single numbers are not included),
then you would use a function to calculate the sum of a group of numbers and return True or False
def return_sum(number_list, compare_to):

and would use a for loop(s) to group the numbers, that is, send groups of 2, 3, etc to the function for comparison. Start with the individual parts. Use a loop to divide the list into groups, send each one to the function, and do something with the result.

i ended up figuring this out using itertools.combination

If you have time, post the solution you came up with.

If you have time, post the solution you came up with.

Sure here you go..

from itertools import cominations

def getCombos(somelist, x):
    x += 1
    for n in range(1,x):
        gen = combinations(somelist,n)
        while True:
            try:
                combo = gen.next()
                yield combo
            except StopIteration:
                break

example of it working:

>>> data = getCombos([1,2,3,4],3)
>>> 
>>> while True:
	try:
		print data.next()
	except StopIteration:
		break

	
(1,)
(2,)
(3,)
(4,)
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)

Missing the value part.

Missing the value part.

Yeah i really have not used that yet in my code, although i planed to...

but u could do it like this:

def getCombos(somelist, x):
    x += 1
    for n in range(1,x):
        gen = combinations(somelist,n)
        while True:
            try:
                combo = gen.next()
                if sum(combo) == value:
                    yield combo
            except StopIteration:
                break

it working:

>>> value = 4
>>> data = getCombos([1,2,3,4],2)
>>> data.next()
(4,)
>>> data.next()
(1, 3)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.