Here are some exercises for you to try on your own. Solutions are available at http://pragprog.com/titles/gwpy3/practical-programming.
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.
In this exercise, you’ll develop a function that finds the minimum or maximum value in a list, depending on the caller’s request.
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.
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?
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.
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.
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.