My exercise code for problem in the book Beauty of Programming