Solved problem 6 with Python