Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Exercises

  1. Specify the set of values and the set of operations provided by each of the following Python types:
    1. int,
    2. float, and
    3. str.
  2. What are the features of Python that facilitate the creation of user-defined data types.
  3. Explain how each of the following Python features supports polymorphism:
    1. classes,
    2. inheritance, and
    3. special methods like __cmp__ and __ismember__.
  4. Suppose we define two concrete classes, A and B, both of which are derived from the abstract Object class declared in Program gif. Furthermore, let a and b be instances of classes A and B (respectively) declared as follows:
    class A(Object):
        ...
    class B(Object):
        ...
    a = A()
    b = B()
    Give the sequence of methods called in order to evaluate a comparison such as ``a < b''. Is the result of the comparison True or False? Explain.
  5. Let c be an instance of some concrete class derived from the Container class given in Program gif. Explain how the statement
    print str(c)
    prints the contents of the container on the console.
  6. Suppose we have a container c (i.e., an instance of some concrete class derived from the Container class defined in Program gif) which among other things happens to contain itself. What happens when we invoke the __str__ method on c?
  7. Iterators and visitors provide two ways to do the same thing--to visit one-by-one all the objects in a container. Give an implementation for the accept method of the Container class that uses an iterator.
  8. Is it possible to implement an iterator using a visitor? Explain.
  9. Suppose we have a container which we know contains only plain integers. Design a Visitor which computes the sum of all the integers in the container.
  10. Consider the following pair of Associations:
    a = Association(3, 4)
    b = Association(3)
    Give the sequence of methods called in order to evaluate a comparison such as ``a == b''. Is the result of the comparison True or False? Explain.

next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.