Problem 29 in Python