Completed PERM solution in Python.