python algorithm 1.2.1.E