The exercise of algorithm in Python