soma_ll function that receives a list of lists and returns the sum of all numbers

Asked

Viewed 169 times

0

def soma_ll(lista):

     return  lista[0] + soma_ll(lista[1:])

Why doesn’t my test run?

def test_600_soma_ll(self):

    self.assertEqual(soma_ll([1,2,3]),6)
    self.assertEqual(soma_ll([1,2,3,4]),10)
    self.assertEqual(soma_ll([-1,-2,-3,-4]),-10)
    self.assertEqual(soma_ll([1]),1)
    self.assertEqual(soma_ll([]),0)
    self.assertEqual(soma_ll([-3]),-3)
    self.assertEqual(soma_ll([[],[]]),0)
    self.assertEqual(soma_ll([[1],[2]]),3)
    self.assertEqual(soma_ll([[[1,2,3],[4,5],11],9,8]),43)
    self.assertEqual(soma_ll([[[1,2,3],[4,5],11,4],9,8,4]),51)
    self.assertEqual(soma_ll([[-1],[1]]),0)
    self.assertEqual(soma_ll([[1],[[2],1]]),4)

1 answer

1

It doesn’t work because there comes a time when the generated sub-list is empty and a IndexError. For example, if the list is [1, 2]:

  • the sum function lista[0] (paragraph 1) with soma_ll(lista[1:]) (that is to say, soma_ll([2]))
    • soma_ll([2]) summing up lista[0] (paragraph 2) with soma_ll(lista[1:]) (that is to say, soma_ll([]))
      • soma_ll([]) tries to catch lista[0], but since the list is empty, there is no zero index, then there is a IndexError - see

To fix, you must put a stop condition, which is when the list is empty:

def soma_ll(lista):
    if not lista:
        return 0
    return  lista[0] + soma_ll(lista[1:])

print(soma_ll([1, 2, 3])) # 6

If the list is empty, it returns zero (for in an empty list, the sum of its elements - nonexistent - is zero).

if not lista checks if the list is empty, because empty lists are considered False. But you could also do if len(lista) == 0.


And always remember that recursion nay is the best way to add the elements of a list. For this there is already something ready in language, just do sum(lista).


Another detail is that in many cases you are adding lists within lists (within lists, within lists, etc.). In this case you need to check whether the element being summed is a number or a list (and if it is a list, you should call the recursive function again):

def soma_ll(lista):
    if not lista:
        return 0
    if isinstance(lista[0], list):
        sum = soma_ll(lista[0])
    else:
        sum = lista[0]
    return sum + soma_ll(lista[1:])

print(soma_ll([[[1, 2, 3], [4, 5], 11, 4], 9, 8, 4])) # 51
print(soma_ll([[], []])) # 0

Here I simplified a lot, because if it is not list, I consider that it is number.

  • I don’t know if it’s happening my old man is stopping this test self.assertEqual(soma_ll([[],[]]),0) and I’ve modified several times give this is not going through everything.

  • @Rômulosilvasouzasantos I updated the answer

  • Guy fought over the exposure

  • @If the answer solved your problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. And when I get 15 points, you can also vote in all the answers you found useful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.