Refactor problem 5 for python 3