Solves Problem 3 in Python