Python solution of Problem 8.