Solved problem 4 with Python