0

I have this problem E1). L is a list whose elements may be hidden. A hidden element is one that is stored inside one or more lists in the list L. Design a recursive function that displays all the "visible elements" of L

for example For L = [1,[2],[[3]],[4,5],6], the expected result is [1,2,3,4,5,6]. In this example the numbers 1 and 6 are visible and the elements 2, 3, 4 and 5 are hidden.

im learning recursion in python, i tried to find a solution for this problem but i was just able to do this:

l = [1,2,4,5,6]
def simple_vista(l):
  if l==[]:
    return 0
  else:
    if isinstance(l[0], list):
      pass
    else:
      l[0] + simple_vista(l[1:])

print("los numeros a simple vista son: ", simple_vista(l))

The idea that i have to solve the problem:

My idea is to check if the l[0] is a iterable type item (a list) and if is it, ignore it and the same with the rest of elements of the list (l[1:]), if an element is not a iterable type item, save it and at the end, show that numbers.

Note: recursion means that im not able to use cicles for and while in order to answer the problem

  • Can you describe in plain English, not python code, what do you think your code should do? Let's take for example, `if l==[]: return 0`, what's the reasoning behind this? How a list like `L` from the example would produce a case of an empty list, why you think your function should return `0` in that case? – Ignatius Reilly Aug 25 '22 at 04:20
  • the reason why im putting if l==[]: return 0, is because i want to find the items who aren´t in a nested list (the visible elements) , so i learn that for recursion you need to stablish a base case, so if list is empty you will get 0 "visible elements" because list is empty. – Andrés Mendoza Villalba Aug 25 '22 at 04:27
  • You're right about the base cases. But I think the objective is to return the elements, not the number of elements. So, in your case, if you were to get an empty list `[]`, your function would return `0`, not the empty list itself. – Ignatius Reilly Aug 25 '22 at 04:30
  • Also, an empty list would be a extreme case. Let's start by working on a list that always have elements, like the one called `L` in your example. In each position, there's either a number, or a list that's not empty (it has other numbers or other lists). – Ignatius Reilly Aug 25 '22 at 04:32
  • i dont understand, im not returning 0 when list is empty ? – Andrés Mendoza Villalba Aug 25 '22 at 04:41
  • Yes, but you should return the values, not how many values there are. – Ignatius Reilly Aug 25 '22 at 04:42
  • [Here's a simple solution](https://stackoverflow.com/a/12472564/15032126), and [this one](https://stackoverflow.com/a/4590652/15032126) is a very elegant one. – Ignatius Reilly Aug 25 '22 at 05:03
  • actually my solution makes one less recursive call than the one in your link so It is probably more efficient than that – Alexander Aug 25 '22 at 05:08
  • @Alexander, you don't use recursion to solve this problem because of efficiency. Recursion is a bad solution anyway for this problem as noted in the discussions of one of the [duplicates](https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists). The linked solution is more elegant. – Ignatius Reilly Aug 25 '22 at 05:11
  • @IgnatiusReilly So in your view, if you are using recursion then efficiency no longer matters? – Alexander Aug 25 '22 at 05:12
  • Also, you have been here long enough to know both how to handle [HW questions](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) and that you should avoid duplicate answers. – Ignatius Reilly Aug 25 '22 at 05:13
  • @Alexander in my view the objective *here* is pedagogy, not efficiency. The solutions I linked, they both have a certain level of complexity (double recursion or nested functions) that's in the reach of the OP's skills and more "interesting" than just code that works. – Ignatius Reilly Aug 25 '22 at 05:15
  • @IgnatiusReilly I totally agree which is precisley why I chose to use the OP's code as a starting point and is also why I didn't just answer with a block of code that works. So thankfully we can agree on that. – Alexander Aug 25 '22 at 05:19

1 Answers1

0

This should work. We can actually achieve the same thing using your code as a starting point.

L = [1,[2],[[3]],[4,5],6]

def simple_vista(l):
    if l==[]:
        return []  # return empty list from base case
    out = []
    if isinstance(l[0], list):  # check if the first element is a list
        out += simple_vista(l[0] + l[1:])  # recursive call
    else:
        out += [l[0]] + simple_vista(l[1:])  # otherwise add it to 
                                             # the output and recurse the rest
    return out

OUTPUT

[1, 2, 3, 4, 5, 6]
Alexander
  • 16,091
  • 5
  • 13
  • 29