Implementation of Tiernan's cycle detection algorithm, in Python 2