produces correct solution to sample input for exercise 23