University of Virginia Computer ScienceCS216: Program and Data Representation, Spring 2006 |
(none)06 February 2006 |

To solve a problem recursively:

- Be optimistic.
- Assume you can solve it.
- If you could, how would you solve a bigger problem.

- Think of the simplest version of the problem, something you can already solve. (This is the base case.)
- Combine them to solve the problem.

To solve a problem involving list data recursively:

- Be
*very*optimistic. Since lists themselves are recursive structures, we should be able to come up with a recursive definition for almost anything we can think of doing with a list.- Assume we can solve a smaller version of the problem.
- Break the problem into the first element of the list and the rest of the list.

- Think of the simplest version of the problem, something you can solve already. For lists, this is usually the empty list. (Sometimes, it might be the length 1 list.)
- Combine them to solve the problem.

Many recursive list procedures can be defined with this template:

For example:deflistproc(lst): if lst.__next == None:what to do for a one element listelse:f(g(lst[0]), lst.__next.listproc()

What aredef length (self): if self.__next == None: return 1 else: return 1 + self.__next.length ()

Define thedefsumlist (self):ifself.__next == None:return________________________________________else:return______________________ + self.__next.________________________

deffilterMatches (self, el):ifself.__next == None:ifself.__info == el: __________________________________else: return selfelse:ifself.__info == el: return filterMatches (self.__next, el)else:

To solve a problem involving trees recursively:

- Be
*extremely*optimistic. Since trees themselves are recursive structures, we should be able to come up with a recursive definition for almost anything we can think of doing with a tree.- Assume we can solve a smaller version of the problem.
- Break the problem into the root node and the children of that node.

- Think of the simplest version of the problem, something you can solve already. For trees, this is usually a leaf or the empty tree.
- Combine them to solve the problem.

Finish this definition that finds the maximal value in a tree:defsize(self): count = 1 # count this nodeforchild in children(self): count += child.____________________returncount

What is the running time ofdeflargest(self): __________________________________forchild in children(self):ifchild.____________________ > max: max = child.largest()returnmax

Define a procedure that implements `largest` whose running time
is in *O*(*n*):

deflargest (self):

What are the possible lists that can be created using the elements {1, 2, 3}?

Define a generator that generates all possible lists (here we use
the Python list datatype, not the list types from PS2). (If you are
stuck, see a partial solution on the next page.)

def allLists (elements): if len(elements) == 1: yield ___________________ else: for index in range(len(elements)): # pick this element first first = elements[index] # rest is a copy of the original list with first removed rest = elements[:] del rest[index] assert (len(rest) == len(elements) - 1) for rlist in allLists (rest): if not rlist == None: ____________________________ yield rlist[an error occurred while processing this directive]