Quiz 9(b) Thu 25 Oct 2007

  1. Design a class histogram to count the number of times words occur in a text. What are the data and function attributes? Give one line description for every attribute.

    Answer:

    The class data attribute of the histogram class is a dictionary. The two functional attributes are the add, to add a new word, and show, to show the histogram.

    The structure of the class is as follows:

    class Histogram:
       "keeps track of words"
    
       d = {}
    
       def add(self,word):
          "adds a word to the dictionary"
    
       def show(self):
          "shows the histogram"
    
  2. Write Python code to define the class and its data attributes. For each function in the class, give only its name, parameters, and its documentation string.

    Answer: class histogram with main interactive test program.