python algorithm from books