Problem 18 in Python