Solves Problem 2 in Python