Chapter 7 Exercise Set 0: Chapter Review¶
Doctest Exercises¶
In each of the following exercises, write Python code to make the doctests pass.
class Foo: """ >>> foo = Foo(42, 'eggs') >>> foo.num 42 >>> foo.food 'eggs' >>> foo.message() 'I love eggs!' """
class Bar: """ >>> bar = Bar() >>> bar.this 'Ni!' >>> bar.that 'shrubbery' >>> bar.speak(2) 'Ni! Ni!' >>> bar.speak(5) 'Ni! Ni! Ni! Ni! Ni!' """
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)¶
Write a
distancefunction that takes twoPoints 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 """
Rewrite your
distancefunction as a method inside thePointclass. 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.pythat contains the completePointclass as described in the chapter.
Time¶
Printing Times¶
Rewrite the
print_timemethod from the chapter so that it overloads the built-inprintfunction and add it to theTimeclass described in the chapter in a module namedmy_time.py. Be sure your new method returns a string instead of callingprint.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¶
Rewrite the
incrementmethod so that it doesn’t contain any loops.Now rewrite
incrementas a pure function, and write method calls to both versions.
convert_to_seconds¶
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
Timeclass.
find¶
Add a fourth parameter,
end, to thefindfunction that specifies where to stop looking. Warning: This exercise is a bit tricky. The default value ofendshould belen(str), but that doesn’t work. The default values are evaluated when the function is defined, not when it is called. Whenfindis defined,strdoesn’t exist yet, so you can’t find its length.
NumberSet¶
Fill in the class
NumberSetwith 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] """