Exercises

Here are some exercises for you to try on your own. Solutions are available at http://pragprog.com/titles/gwpy3/practical-programming.

  1. Your lab partner claims to have written a function that replaces each value in a list with twice the preceding value (and the first value with 0). For example, if the list [1, 2, 3] is passed as an argument, the function is supposed to turn it into [0, 2, 4]. Here’s the code:

     from​ typing ​import​ List
     
     def​ double_preceding(values: List[float]) -> None:
     """Replace each item in the list with twice the value of the
      preceding item, and replace the first item with 0.
     
      >>> L = [1, 2, 3]
      >>> double_preceding(L)
      >>> L
      [0, 2, 4]
      """
     
     
     if​ values != []:
      temp = values[0]
      values[0] = 0
     for​ i ​in​ range(1, len(values)):
      values[i] = 2 * temp
      temp = values[i]

    Although the example test passes, this code contains a bug. Write a set of unittest tests to identify the bug. Explain what the bug in this function is, and fix it.

  2. Your job is to come up with tests for a function called line_intersect, which takes two lines as input and returns their intersection. More specifically:

    • Lines are represented as pairs of distinct points, such as [[0.0,0.0], [1.0, 3.0]] .

    • If the lines don’t intersect, line_intersect returns None.

    • If the lines intersect in one point, line_intersect returns the point of intersection, such as [0.5, 0.75].

    • If the lines are coincident (that is, lie on top of each other), the function returns its first argument (that is, a line).

    What are the six most informative test cases you can think of? (That is, if you were allowed to run only six tests, which would tell you the most about whether the function was implemented correctly?)

    Write out the inputs and expected outputs of these six tests, and explain why you would choose them.

  3. Using unittest, write four tests for a function called all_prefixes in a module called TestPrefixes.py that takes a string as its input and returns the set of all nonempty substrings that start with the first character. For example, given the string "lead" as input, all_prefixes would return the set {"l", "le", "lea", "lead"}.

  4. Using unittest, write the five most informative tests you can think of for a function called is_sorted in a module called TestSorting.py that takes a list of integers as input and returns True if they are sorted in nondecreasing order (as opposed to strictly increasing order, because of the possibility of duplicate values), and False otherwise.

  5. The following function is broken. The docstring describes what it’s supposed to do:

     def​ find_min_max(values: list):
     """Print the minimum and maximum value from values.
      """
     
      min = None
      max = None
     for​ value ​in​ values:
     if​ value > max:
      max = value
     if​ value < min:
      min = value
     
     print​(​'The minimum value is {0}'​.format(min))
     print​(​'The maximum value is {0}'​.format(max))

    What does it actually do? What line(s) do you need to change to fix it?

  6. Suppose you have a data set of survey results where respondents can optionally give their age. Missing values are read in as None. Here is a function that computes the average age from that list:

     from​ typing ​import​ List
     
     def​ average(values: List[float]) -> float:
     """Return the average of the numbers in values. Some items in values are
      None, and they are not counted toward the average.
     
      >>> average([20, 30])
      25.0
      >>> average([None, 20, 30])
      25.0
      """
     
      count = 0 ​# The number of values seen so far.
      total = 0 ​# The sum of the values seen so far.
     for​ value ​in​ values:
     if​ value ​is​ ​not​ None:
      total += value
     
      count += 1
     
     return​ total / count

    Unfortunately it does not work as expected:

     >>>​​ ​​import​​ ​​test_average
     >>>​​ ​​test_average.average([None,​​ ​​30,​​ ​​20])
     16.666666666666668
    1. Using unittest, write a set of tests for function average in a module called test_average.py. The tests should cover cases involving lists with and without missing values.

    2. Modify function average so it correctly handles missing values and passes all of your tests.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.216