Solution 8 in Python.