Lecture 23 — Exercises

Solutions to the problems below must be sent to Submitty for automatic scoring. A separate file must submitted for each problem. Start by downloading lec23_ex.zip from the Piazza site. Solutions must be submitted by 4 pm on Friday, December 8.

  1. The following code recursively calculates the maximum value in a list.

    def recursive_max_impl( L, i ):
        The actual recursive function.
        if i == len(L)-1:
            return L[i]
            return max(L[i], recursive_max_impl(L,i+1) )
    def recursive_max( L ):
        The driver for the recursive function.  This handles the special
        case of an empty list and otherwise makes the initial call to the
        recursive function.
        if len(L) == 0:
            return -99999    # By convention
            return recursive_max_impl( L, 0 )
    if __name__ == "__main__":
        L1 = [ 5 ]
        print(recursive_max( L1 ))
        L2 = [ 24, 23.1, 12, 15, 1 ]
        print(recursive_max( L2))
        L2.append( 55 )
        print(recursive_max( L2 ))

    Using this as a guide, write a recursive function to add the values in a list. You should have to change very little code. Implement your code in the provided file prob1.py

  2. Implement a recursive solution to the Fibonacci number function definition given during lecture. Implement your code in the provided file prob2.py.

  3. Looking carefully at the Fibonacci definition shows that calculating Fibonacci number \(f_{n-1}\) requires calculating Fibonacci number \(f_{n-2}\), which is also required for calculating Fibonacci number \(f_n\). This means there is redundant computation. This redundancy gets worse for numbers \(f_{n-3}\), \(f_{n-4}\), etc.

    Fortunately, the Fibonacci sequence is relatively easy to compute non-recursively. That is your problem here. The trick is to build up the solution using a for loop that calculates \(f_2\), then \(f_3\), then \(f_4\), etc. Implement your solution in prob3.py.

    Out of curiosity, you could run your solutions to the previous two problems on large values of n and time the difference.