[python] add detecting cycles
[python] add stack implementation
[python] add mth to last element