Solves Problem 12 in Python