Solved problem 5 with Python