How can I do this recursive function

Asked

Viewed 155 times

-2

Implement a recursive file_recursive function it receives a list and a number and returns the list, except every time the number appears

For example filtro_recursivo([0,1,2,1,4],1) returns [0,2,4]
For example filtro_recursivo([0,1,2,1,4],4) returns [0,1,2,1]
For example filtro_recursivo([0,1,2,1,4],5) returns [0,1,2,1,4]
For example filtro_recursivo([],5) returns []

The first test takes the "simple case": lists with 0 or 1 element After passing it, implement recursion using the two ideas below

  • filtro_recursivo([5, resto],5) = filtro_recursivo(resto);
  • filtro_recursivo([8, resto],5) = [8]+filtro_recursivo(resto);

I implemented so only that did not pass the test

def filtro_recursivo(lista, numero):
    for i in range(0, len(lista)):
        if lista [i] == numero:
            lista.pop(i)
    return lista
  • What is this identifier resto?

  • is the rest give recursion .

  • Rest of the recursion? Doesn’t make any sense

  • How can you get the rest of an operation that has not yet occurred?

  • If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

2 answers

2

The concept of recursive use is mistaken.

In my view in this exercise recursion is asked to functionally iterate through the list elements and discard the first element each new iteration. This means that one should not make use of iteration loops while or for.

def filtro(lista, numero):     
  if len(lista) == 0: return lista       #Se a lista for vazia a retorna terminando o ciclo de iterações
  # Se lista[0] = numero retorna o resultado da filtragem de lista[1] até o final da lista
  #Senão insere lista[0] o resultado da filtragem de lista[1] até o final da lista
  return filtro(lista[1:], numero) if lista[0] == numero else [lista[0]] + filtro(lista[1:], numero)

Test in Repl.it

The same code only more readable

def filtro(lista, numero):    
    
  if len(lista) == 0: return lista       #Se a lista for vazia a retorna terminando o ciclo de iterações      
  resultado = filtro(lista[1:], numero)  #Faz a filtragem recursiva da lista menos o primeiro elemento
  #Se lista[0] != numero... 
  if lista[0] != numero:
     resultado.insert(0, [lista[0]]);    #...insere no resultado o primeiro elemento da lista
  return resultado 
 

1

What you should do is check within the function whether there is value or not in the list. If there is a value, the function returns the result of the same function by passing the same parameters.

If the value no longer exists in the list, the function will return the list without modification. You can also use the method copy() so that the original list is not affected.

def filtro(lista, value):
    lista = lista.copy()

    if value in lista:
        lista.remove(value)
        return filtro(lista, value)

    return lista

print(filtro([1, 0, 2, 1, 4, 5, 3, 2, 0], 2))  # [1, 0, 1, 4, 5, 3, 0]

Still, I don’t think it’s a good idea to create a recursive function for this. A simple loop while already solves this problem:

def filtro(lista, value):
    lista = lista.copy()

    while value in lista:
        lista.remove(value)

    return lista

Browser other questions tagged

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