Sebastien Boulas
Sebastien Boulas

Reputation: 21

Why is this recursive function not processing the entire list?

Can someone tell me what is wrong with the code I am writing? I am trying to find a sum of all elements in this list. My code:

def determine_sum(m_list, sum_count): 

    for i in range(len(m_list)): 

        if isinstance(m_list[i], int):      
            sum_count += m_list[i]  


        else:  
            return determine_sum(m_list[i], sum_count)

    return sum_count 

m_list = [1, 2, 3, [4, 5], [4, [3,4]] ]

print(determine_sum(m_list, 0))

The output is 15, but I expected 26.

Upvotes: 1

Views: 52

Answers (3)

Grismar
Grismar

Reputation: 31329

The main problem with your code is this line:

return determine_sum(m_list[i], sum_count)

If you run into another list, you call determine_sum recursively, which computes the sum of the sub-list and starts with a count of sum_count. However, whatever the result is for the first sub-list, is the total result you're returning. Your function ignores any following lists, because return ends the function.

A recursive function like this doesn't need to pass sum_count around. Instead, you can just compute the sum of a sub-list recursively and then add the result to the current sum, continuing the loop. Every call of determine_sum will get its own copy of the running sum, so you don't need to pass it.

Like this:

def determine_sum(m_list): 
    sum_count = 0

    for i in range(len(m_list)): 
        if isinstance(m_list[i], int):      
            sum_count += m_list[i]  
        else:  
            sum_count += determine_sum(m_list[i])

    return sum_count 

m_list = [1, 2, 3, [4, 5], [4, [3,4]] ]

print(determine_sum(m_list))

So, you weren't far off.

Note: some of the other suggested solutions keep the parameter sum_count and change the problematic line to:

            sum_count = determine_sum(m_list[i], sum_count)

That also works, but is only recommended if you're writing a function that's intended to add the recursive sum of lists to some existing number. Given the name of your function, it doesn't seem like that's really your aim.

Upvotes: 0

Michael Bianconi
Michael Bianconi

Reputation: 5242

def determine_sum(m_list, sum_count): 

    for i in range(len(m_list)): 

        if isinstance(m_list[i], int):      
            sum_count += m_list[i]  

        else:
            # Don't return here, just add the count and keep looping
            sum_count = determine_sum(m_list[i], sum_count)

    return sum_count 

Upvotes: 2

Kostas Minaidis
Kostas Minaidis

Reputation: 5422

Getting the sum of the sample list: 26

You were missing to add the output of the nested determine_sum in the sum_count.

def determine_sum(m_list, sum_count): 

    for i in range(len(m_list)): 
        if isinstance(m_list[i], int):      
            print(m_list[i])
            sum_count += m_list[i]  
        else:  
            sum_count = determine_sum(m_list[i], sum_count)
    return sum_count 

m_list = [1, 2, 3, [4, 5], [4, [3,4]] ]

print("Sum:", determine_sum(m_list, 0)) # 26

Upvotes: 1

Related Questions