0

I need to define a function that finds the numbers within a list that add up to a given sum. I want to do this function recursively.

This is what I have so far, I think I need to work on my recursion and base cases.

def findsum ( x , y ) :

    pile = []
    z = x-y[0]
    if x == 0 :
            return pile
    elif y == [] :
            return pile
    else:
            index = 0
            n = len ( y )
            while index < n:
                    if sum( y[:index]) == x - y[index]:
                            pile += y[index]
                            y = y[:index]
                            x = x - y[index]
                    index += 1
            return pile + findsum ( x , y )

How can I edit this to find the the numbers in list y that add up to the sum x while using recursion.

  • *subset sum* is a pretty hard problem to solve (it's NP-complete)... – nneonneo Oct 14 '12 at 20:30
  • This question is already answered. Check out the following link: http://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum – ashish makani May 20 '13 at 21:51

0 Answers0