15-112 Lecture 8 (July 12, 2012)

Recursion Practice

Today, among other things, we walked through a few simple recursion examples. Please find below those examples and a couple of more.

Blobs, Solved

We talked a bunch about how to solve "Blobs" in Python. And, we wrote the recursive method on the board. But, we never put all of the pieces together, including reading in the map and also actually marking the 2D list. Please find the full implementation below, including a sample map of blobs, below.

Note the use of the "copy.deepcopy()" method to copy the grid to make the marking grid. This method makes a copy of the list, including the nested lists. A "shallow" copy() would copy the outer list and "point at" the inner lists. A simple assignment would "point at" the outer list.

Notice also that, since I put my functions in different files, I had to import them, using their file name as the module name. Subsequently, I had to prefix the imported methods with their module name, e.g. copyMap.copyMap(...)

It is probably also worth noting that I used a non-recursive "wrapper" function to hide some of the state that recursion needs from the user. This wrapper function calls the recursive function -- with the right initial state passed in.