Solved problem 9 in python