Solved problem 21 in Python