Problem 2 solved using Python