Problem 6 solved in Python.