Solved problem 3 with Python