Most pythonic way of rotating a circular list to a canonical point

Oscar Benjamin oscar.j.benjamin at gmail.com
Sat Aug 1 19:05:03 EDT 2015


On Sat, 1 Aug 2015 22:06 Lukas Barth <mail at tinloaf.de> wrote:



Nice idea! But I actually really need those "canonic rotations", since I'm
hashing them somewhere.



Do you really need the canonical rotation or just a hash that is invariant
under rotations?

I don't know of a solution to the former that is better than what you
already have but the latter is an easier problem: Find the minimum element.
Compute the hash of the rotated sequence for each occurrence of the least
common element. Add those hashes together or multiply them or some similar
operation. That's your hash that will compare equal for any rotation of a
given sequence.

--
Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150801/f024a10d/attachment.html>


More information about the Python-list mailing list