Solves Problem 4 in Python