Solved problem 7 in python