Show the contents of a list recursively

1

We are studying recursion in our programming course, and we have an exercise where we have to show in idle the contents of a list using recursion. I did it like this:

def print_list_r(ls=[], n=0):
    if len(ls) == 0 or n < 0:
        return False
    if n < len(ls):
        print(ls[n])
    else:
        return
    print_list_r(ls, n+1)

Is this a good algorithm? Is it possible to do this algorithm without using the parameter n ?

    
asked by anonymous 07.11.2014 / 00:50

1 answer

1

Yes, it's a good algorithm. All relevant tests have been done (stop condition ok, limit ok indexes), in fact the test by len(ls) == 0 is half redundant: if n >= 0 - condition necessary to pass the first if - is true, then the condition n < len(ls) is only true if len(ls) > 0 . There is no unnecessary copy of objects (the same list is always used) and is still present at tail recursion - if the implementation takes advantage of this, your code can be as efficient as an iterative (unfortunately does not seem to be the case with CPython).

What I did not understand is why you return False if the list is empty and None otherwise. I even understand that you might want to give a different output if n is invalid (negative), but an empty list, in my view, should be a valid entry - treated indistinctly from the other entries.

How much to do without using the parameter n , it is possible, although inefficiently:

def print_list_r(ls=[]):
    if len(ls) > 0:
        print(ls[0])
        print_list_r(ls[1:])

That is, if the list is not empty, print the first element and call the function recursively passing as an argument the sublist with the indexes 1 onwards (ending with an empty sublist).

    
07.11.2014 / 01:48