Solved problem 16 in python