|
University of Virginia Computer Science CS216: Program and Data Representation, Spring 2006 |
(none) 06 February 2006 |
To solve a problem recursively:
To solve a problem involving list data recursively:
Many recursive list procedures can be defined with this template:
def listproc (lst):
if lst.__next == None:
what to do for a one element list
else:
f (g(lst[0]), lst.__next.listproc ()
For example:
def length (self):
if self.__next == None:
return 1
else:
return 1 + self.__next.length ()
What are f and g to match the template?
Finish the sumlist procedure defintion below. It should take a
ListNode object as its parameter and evaluate the the sum of
the values of all elements in the list.
def sumlist (self):
if self.__next == None:
return ________________________________________
else:
return ______________________ + self.__next.________________________
Define the filterMatches procedure that takes a ListNode and
an element, and evaluates to the list containing all elements from the
original list that do not match el.
def filterMatches (self, el):
if self.__next == None:
if self.__info == el:
__________________________________
else:
return self
else:
if self.__info == el:
return filterMatches (self.__next, el)
else:
Recusive Definitions on Trees
To solve a problem involving trees recursively:
def size(self):
count = 1 # count this node
for child in children(self):
count += child.____________________
return count
Finish this definition that finds the maximal value in a tree:
def largest(self):
__________________________________
for child in children(self):
if child.____________________ > max:
max = child.largest()
return max
What is the running time of largest defined above?
Define a procedure that implements largest whose running time
is in O(n):
def largest (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]