Problem 3 implemented in Python.