Solves Problem 35 in Python