Completed problem 18 for Python