Problem 7 solved in Python.