Problem 2 solution in Python