0

I have been trying to write code which gives the solution of the number of ways of reaching a sum, which is specified. This is very similar to the subset sums problem which I have found online (Finding all possible combinations of numbers to reach a given sum).

I modified slightly the code so that it reuses numbers multiple times.

object_list = [(2, 50), (3, 100), (5, 140)] # the first number in the tuple is my weight and the second is my cost
max_weight = 17
weight_values = [int(i[0]) for i in object_list]
cost_values = [int(i[1]) for i in object_list]


def subset_sum(objects, max_weight, weights=[]):
    w = sum(weights)


    if w == max_weight:
        print("sum(%s)=%s" % (weights, max_weight))
    if w >= max_weight:
        return  

    for i in range(len(objects)):
        o = objects[i]
        subset_sum(objects, max_weight, weights + [o])


if __name__ == "__main__":
    subset_sum(weight_values, max_weight)



print(subset_sum(weight_values, max_weight))

This gives the solution:

sum([2, 2, 2, 2, 2, 2, 2, 3])=17
sum([2, 2, 2, 2, 2, 2, 3, 2])=17
sum([2, 2, 2, 2, 2, 2, 5])=17
sum([2, 2, 2, 2, 2, 3, 2, 2])=17
...

So on so forth. Unlike the original I am using a list of tuples and then taking the first value of the tuple to make a list. This is the same with the last value.

The part I am currently stuck on is how to store these values and reuse them in the next part of the code.I had a look at this post but I couldn't understand it (Python: How to store the result of an executed function and re-use later?).

So I want to store the part of the solution which stores [2, 2, 2, 2, 2, 2, 2, 3] from the solution sum([2, 2, 2, 2, 2, 2, 2, 3])=17. I want to do this for all solutions because in the next step i am going to replace the numbers with the next part of the tuple (so 2 will be replaced by 50 because the tuple that 2 is in is (2,50)). Then I am going to use this to print another sum value with the replaced numbers and print the highest value (probably going to sort the solutions from highest to lowest and print the first value in the list)

I tried using a dictionary to try and replace the values after the calculation but i couldn't manage to do it.

I tried:

dictionary = dict(zip(weight_values, cost_values))

Any help is appreciated. before anyone asks Ia have looked online for solutions and have no one else to ask help from, since the only person i know who has a background in coding is my brother who isn't at home

Sayse
  • 42,633
  • 14
  • 77
  • 146
A0sXc950
  • 127
  • 1
  • 10
  • I'm not sure I entirely understand your question, but it seems like you just need to `return` the values from the function to use them where the function was called. Right now, all your `return` is doing is ending the function; it's not actually returning any values. – Carcigenicate Dec 10 '17 at 20:03
  • 1
    I suspect that what you _really_ need here is to create a recursive generator. To do that, you'll need to learn how `yield` and `yield from` work. [This answer](https://stackoverflow.com/a/47618187/4014959) I wrote a few days ago is a relatively simple example, and it's related to your current task. – PM 2Ring Dec 10 '17 at 20:19
  • @Carcigenicate Yeah i noticed that too but according to the original poster of the code that is what is supposed to happen. – A0sXc950 Dec 10 '17 at 21:17
  • @PM2Ring Thank you for this. I feel by looking at your example I can learn from it and implement my own version. – A0sXc950 Dec 10 '17 at 21:19

0 Answers0