Port the algorithm to Python 3.