Exercises

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

  1. A DNA sequence is a string made up of the letters A, T, G, and C. To find the complement of a DNA sequence, As are replaced by Ts, Ts by As, Gs by Cs, and Cs by Gs. For example, the complement of AATTGCCGT is TTAACGGCA.

    1. Write an outline in English of the algorithm you would use to find the complement.
    2. Review your algorithm. Will any characters be changed to their complement and then changed back to their original value? If so, rewrite your outline. Hint: Convert one character at a time, rather than all of the As, Ts, Gs, or Cs at once.
    3. Using the algorithm that you have developed, write a function named complement that takes a DNA sequence (a str) and returns the complement of it.
  2. In this exercise, you’ll develop a function that finds the minimum or maximum value in a list, depending on the caller’s request.

    1. Write a loop (including initialization) to find both the minimum value in a list and that value’s index in one pass through the list.
    2. Write a function named min_index that takes one parameter (a list) and returns a tuple containing the minimum value in the list and that value’s index in the list.
    3. You might also want to find the maximum value and its index. Write a function named min_or_max_index that has two parameters: a list and a bool. If the Boolean parameter refers to True, the function returns a tuple containing the minimum and its index; if it refers to False, it returns a tuple containing the maximum and its index.
  3. In The Readline Technique, you learned how to read some files from the Time Series Data Library. In particular, you learned about the Hopedale data set, which describes the number of colored fox fur pelts produced from 1834 to 1842. This file contains one value per year per line.

    1. Write an outline in English of the algorithm you would use to read the values from this data set to compute the average number of pelts produced per year.
    2. Translate your algorithm into Python by writing a function named hopedale_average that takes a filename as a parameter and returns the average number of pelts produced per year.
  4. Write a set of doctests for the find-two-smallest functions. Think about what kinds of data are interesting, long lists or short lists, and what order the items are in. Here is one list to test with: [1, 2]. What other interesting ones are there?

  5. What happens if the functions to find the two smallest values in a list are passed a list of length one? What should happen, and why? How about length zero? Modify one of the docstrings to describe what happens.

  6. One or more of the three functions to find the two smallest values don’t work if there are duplicate values, and particularly if the two smallest values are the same. Write doctests to demonstrate the problem, run them, and fix the algorithms that exhibit this bug.

  7. This one is a fun challenge.

    Edsgar Dijkstra is known for his work on programming languages. He came up with a neat problem that he called the Dutch National Flag problem: given a list of strings, each of which is either ’red’, ’green’, or ’blue’ (each is repeated several times in the list), rearrange the list so that the strings are in the order of the Dutch national flag—all the ’red’ strings first, then all the ’green’ strings, then all the ’blue’ strings.

    Write a function called dutch_flag that takes a list and solves this problem.

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

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