Solves Problem 27 in Python