Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with this exercise using recursion in Python?

Tags:

python

I am struggling to solve an exercise regarding lists in Python. The exercise says:

Write a function that takes a list containing numbers and lists of numbers inside them, reverses it (including the lists of numbers inside the main list) using recursion.

Afterwards, the function should return a tuple containing three elements. The first element represents the amount of all the numbers present in the list (including the ones in the sublists), the second element is the sum of all the numbers (also the ones inside the sublists), and the third element is a sorted list of all the integers. Again we should use recursion.

I managed to reverse the whole list using two functions, but when it comes to accessing the numbers inside the sublists to use them for the tuple, I really don't know what to do.

Here you can have a look at the list of lists:

lista = [3, 3, 5, [[1, 8, [9, 3]], 3, [2, [9, [5, 6],[9]] ] ]]

Here you can check my code:

def exercise (lista): 
    lista_ordinata = []
    count = 0
    somma = 0
    reverse_list(lista) 

    for x,y in enumerate(lista):
         if isinstance (x,(int)):
            count += 1
         else:
            count = 0
    for num in lista:
         if isinstance(num,(int)):
            somma += num

    for i in lista:
         if isinstance(i,int):
            lista_ordinata.append(i)
        
    return (count,somma,lista_ordinata)

def is_list (lista):
    return isinstance(lista,list)

def reverse_list(lista):
    lista_nuova = lista[::-1]
    for x,y in enumerate(lista_nuova):
        if is_list(y):
            lista_nuova[x] = reverse_list(y)
    lista.clear()
    lista.extend(lista_nuova)
    return lista

Here you can see the expected list which I reversed:

lista = [[[[[9], [6, 5], 9], 2], 3, [[3, 9], 8, 1]], 5, 3, 3]

The function must return the following tuple:

(13,66, [1,2,3,5,6,8,9])

The output I get is incorrect:

(4, 11, [5, 3, 3])

The first element should be the counting of all the numbers, and not just the numbers outside the sublists, The sum is also incorrect, The list is not outputting all the numbers.

What should I do? Keep in mind that the "exercise" function should use recursion.

like image 382
alex108 Avatar asked Nov 01 '25 11:11

alex108


2 Answers

You are using recursion just fine in the reverse function, just use recursion in you exercise function:

def exercise(lista): 
    lista_ordinata = []
    count = 0
    somma = 0
    # reverse_list(lista)
    
    for x in lista:
        if isinstance(x, list):
            recursion = exercise(x) # call recursion until you have a list with only integers
            # and add the result to your running totals
            count += recursion[0]
            somma += recursion[1]
            lista_ordinata.extend(recursion[2])
        else:
            count += 1
            somma += x
            lista_ordinata.append(x)
    
    lista_ordinata = sorted(list(set(lista_ordinata)))
    
    return count, somma, lista_ordinata

print(exercise(lista))

(13, 66, [1, 2, 3, 5, 6, 8, 9])
like image 95
RichieV Avatar answered Nov 04 '25 01:11

RichieV


def exercise(a, polish=sorted):
  if not isinstance(a, list):
    return 1, a, {a}
  a.reverse()
  amount, sum, numbers = 0, 0, set()
  for b in a:
    a, s, n = exercise(b, set)
    amount += a
    sum += s
    numbers |= n
  return amount, sum, polish(numbers)

Or with a little helper doing the reversals and collecting the numbers:

def exercise(a):
  def go(a):
    if isinstance(a, list):
      a.reverse()
      for b in a:
        go(b)
    else:
      numbers.append(a)
  numbers = []
  go(a)
  return len(numbers), sum(numbers), sorted(set(numbers))

Demo:

lista = [3, 3, 5, [[1, 8, [9, 3]], 3, [2, [9, [5, 6],[9]] ] ]]

>>> exercise(lista)
(13, 66, [1, 2, 3, 5, 6, 8, 9])

>>> lista
[[[[[9], [6, 5], 9], 2], 3, [[3, 9], 8, 1]], 5, 3, 3]
like image 23
superb rain Avatar answered Nov 04 '25 01:11

superb rain