Python solution of Problem 4.