CS 150 - Assignment 9 - Recursion

"To understand recursion, you must understand recursion"

Due: Wednesday 5/3 at the beginning of class

For this assignment, we will be playing with recursion. Note that this assignment is not a group assignment.

Warming up: lists

Having now discovered recursion, you've decided to become a recursion purist and not use any functions that use iteration/loops. To get you started, you're going to implement some of the list functions recursively. In a file called warmup.py, write the following functions using recursion: I've tried to make it clear above what functions are off limits, but please try not to utilize extraneous functions that get around the heart of the problem. If there is any question about what is acceptable, feel free to ask me.

Just for fun

If you want some more practice (not required), you can also try this one:

Big H

In another file called recursive_pictures.py, write a function called recursive_H that takes four parameters: the x and y coordinates of the center of the H, the length of the vertical bars (the horizontal bar will be 2 times the length) and number of recursive levels to draw. Level 1 should just draw an H with four dots at the end, level 2 an H with 4 smaller H's at the end of the vertical bars, etc. The smallest H's will all have black dots at the end of their vertical bars. The following is a level 3 recursive H:

See the prelab for more details.

Once your function works, take a screen shot of your H figure and submit it via the online submission script.



For the last part of this lab add functionality to your "recursive_pictures.py" so that when you run the program/module (but not when you import it) it generates the following picture:

To do this, you must implement a function called stairs that takes three parameters: the x and y coordinates of the bottom left corner of the stairs and the length of the side of the square in the bottom left hand corner. You should recursively draw stairs as long as the side length is greater than 3. The squares will be colored such that they get darker for smaller squares (not grey). Besides drawing the stairs, the function should also return the number of squares inside the stairs, which must be calculated recursively (like we did with counting the lines for broccoli).

Powers of 2 work well for starting lengths. See the prelab for more details on how the stairs can be viewed recursively.


When you're done, create another screen capture of the output of running your program and submit this file along with your .py files (see Lab 2 for instructions on how to do a screen capture if you've forgotten).

Extra points

You may earn up to 2 extra points on this assignment. Below are some ideas, but you may incorporate your own if you'd like. Make sure to document your extra point additions in comments at the top of the file.

When you're done

Make sure that your program is properly commented: In addition, make sure that you've used good style.

Submission procedure

For this assignment, you will have two .py files and two (or more) image files to submit.
Upload each file separately using the submission script on the course page. When asked for the time, enter the total time taken (the same number for each file you upload).


    length                          3
    rec max                         3
recursive pictures.py
    recursive H                     5
    recursive stairs                4
    returns num squares             2
    stairs coloring                 2
    text/generates picture on run   2

Comments, style                     5
Prelab                              2
Extra points                        2

Total                              28 + 2