Complete python algorithm.