I am trying to check if a sum of some numbers in a list adds up to a specific value.

I want to do this:

sums = []

```
for number in numbers:
for sum_ in sums[:]:
sums.append(sum_ + number)
sums.append(number)
return n in sums
```

I am trying to check if a sum of some numbers in a list adds up to a specific value.

I want to do this:

sums = []

```
for number in numbers:
for sum_ in sums[:]:
sums.append(sum_ + number)
sums.append(number)
return n in sums
```

First of all, can you make your goal a bit more clear. I mean what exactly do you want to do?

Do you want the list of sums of all possible combinations of the elements of `global values`

list?

Yes I want to make all the possible combos and store in global values list. Then check if a separate value is in the list.

Here is the error. For every element in list, you are adding another element to the list. It means after every loop there is one more element so that means one more loop that adds one more element and thus the loop runs forever (or until you run out of memory).

I'm working on the solution and I'll put it here as soon as I get and test it.

I think this will only sum the list.

I want to take a list for example {1,2,3,4,5}

sum = [];

and make a new list with the sums of all values.

sum = {1+2,1+3,1+4,1+5, 1}

this is after one iteration

then next iteration the sum list looks like this

{3,4,5,6, 1, 3+2, 4+2, 5+2, 6+2, 1+2}

Would you settle for a yes or no answer, or do you want to see which numbers in the list added up to the given value?

You are trying to solve the 0-1-knapsack problem

on a phone.

Try adding memoization and dividing up your algorithmic phases using a Clock Timer to avoid sending your phone into a coma.

Yeah, I just want a true or false to be returned.

What I want to do:

int num = 8

List = {1,2,3,4,5}

from this list is there a combination of these numbers that add to the variable num.

In this case there is.

It is 5+3 =8

so a True is returned

Here's a recursive solution, with hardly any optimization.

I had to add a Clock Timer for showmanship.

Sample runs ...

I had to add a one shot Clock Timer event to give the

This recursive procedure does all the work, referring back to my 2 globals for efficiency.

I assume only positive items in the list, otherwise the algorithm gets even slower.

Thank you so much for your extensive answer. I will test it out now!

You take the usual "or" block from the drawer, then right-click on it and select "External inputs" from the context menu.

Thank you for the help everyone!!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.