Chapter 7 Exercise Set 0: Chapter Review

Doctest Exercises

In each of the following exercises, write Python code to make the doctests pass.

  1. class Foo:
        """
          >>> foo = Foo(42, 'eggs')
          >>> foo.num
          42
          >>> foo.food
          'eggs'
          >>> foo.message()
          'I love eggs!'
        """
    
  2. class Bar:
        """
          >>> bar = Bar()
          >>> bar.this
          'Ni!'
          >>> bar.that
          'shrubbery'
          >>> bar.speak(2)
          'Ni! Ni!'
          >>> bar.speak(5)
          'Ni! Ni! Ni! Ni! Ni!'
        """
    
  3. class Seqtools:
        """
          >>> myseq = Seqtools([1, 2, 'a', 7, ('x', 'y'), 'yup', 11.3, True])
          >>> myseq.data
          [1, 2, 'a', 7, ('x', 'y'), 'yup', 11.3, True]
          >>> myseq.nums()
          [1, 2, 7, 11.3]
          >>> myseq.seqs()
          ['a', ('x', 'y'), 'yup']
          >>> myseq.others()
          [True]
        """
    

Point

distance(p1, p2)

  1. Write a distance function that takes two Points as parameters and returns the distance between them. It should pass the following doctests:

    def distance(p1, p2):
        """
          >>> p1 = Point(1, 2)
          >>> p2 = Point(4, 6)
          >>> distance(p1, p2)
          5.0
          >>> p3 = Point(16, 11)
          >>> distance(p2, p3)
          13.0
        """
    
  2. Rewrite your distance function as a method inside the Point class. The new doctests should look like this:

    def distance(self, other):
        """
          >>> p1 = Point(1, 2)
          >>> p2 = Point(4, 6)
          >>> p1.distance(p2)
          5.0
          >>> p3 = Point(16, 11)
          >>> p2.distance(p3)
          13.0
        """
    

    Note

    Add this method to a module named point.py that contains the complete Point class as described in the chapter.

Time

Printing Times

  1. Rewrite the print_time method from the chapter so that it overloads the built-in print function and add it to the Time class described in the chapter in a module named my_time.py. Be sure your new method returns a string instead of calling print.

    Once you are finished, you should be able to import your class and do the following:

    >>> from my_time import Time
    >>> t = Time(9, 30, 0)
    >>> print(t)
    9:30:00
    

increment

  1. Rewrite the increment method so that it doesn’t contain any loops.

    Now rewrite increment as a pure function, and write method calls to both versions.

convert_to_seconds

  1. Convert the function convert_to_seconds:

    def convert_to_seconds(t):
        minutes = t.hours * 60 + t.minutes
        seconds = minutes * 60 + t.seconds
        return seconds
    

    to a method in the Time class.

find

  1. Add a fourth parameter, end, to the find function that specifies where to stop looking. Warning: This exercise is a bit tricky. The default value of end should be len(str), but that doesn’t work. The default values are evaluated when the function is defined, not when it is called. When find is defined, str doesn’t exist yet, so you can’t find its length.

NumberSet

  1. Fill in the class NumberSet with data attributes and methods to make the doctests pass.

    class NumberSet:
        """
          >>> nums = NumberSet([2, 4, 6])
          >>> print(nums)
          [2, 4, 6]
          >>> nums.mean()
          4.0
          >>> nums.median()
          4
          >>> nums.mode()
          [2, 4, 6]
          >>> nums2 = NumberSet([1, 2, 6, 6])
          >>> nums2.mean()
          3.75
          >>> nums2.median()
          4.0
          >>> nums2.mode()
          [6]
          >>> numset = [3, 5, 19, 42, 5, 42, 11]
          >>> nums3 = NumberSet(numset)
          >>> nums3.numlist
          [3, 5, 5, 11, 19, 42, 42]
          >>> numset
          [3, 5, 19, 42, 5, 42, 11]
          >>> round(nums3.mean())
          18
          >>> nums3.median()
          11
          >>> nums3.mode()
          [5, 42]
        """