Solves Problem 7 in Python