Problem 4 in python