Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'repeated': 0.07; 'smallest': 0.07; '"""return': 0.09; 'lengths': 0.09; 'lst': 0.09; 'true),': 0.09; 'def': 0.13; 'subject: \n ': 0.15; 'b):': 0.16; 'beginning.': 0.16; 'element,': 0.16; 'elements,': 0.16; 'false),': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'lukas': 0.16; 'operation,': 0.16; 'rotation': 0.16; 'wrote:': 0.16; 'cheap': 0.18; 'comparing': 0.18; 'element': 0.18; 'result,': 0.18; '%s"': 0.22; 'defined': 0.23; '(or': 0.23; 'elements': 0.23; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'appear': 0.26; '(which': 0.26; 'compare': 0.27; 'followed': 0.27; 'hash': 0.29; 'index,': 0.29; 'once,': 0.29; 'unique,': 0.29; 'print': 0.30; 'becomes': 0.30; 'version,': 0.30; 'skip:[ 10': 0.31; 'version:': 0.33; 'lists': 0.34; 'list': 0.34; 'could': 0.35; 'false': 0.35; 'i.e.': 0.35; 'expected': 0.35; 'but': 0.36; 'list,': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'say': 0.37; 'charset:us- ascii': 0.37; 'minimum': 0.38; 'test': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'above,': 0.63; 'positions': 0.64; 'treat': 0.72; '(a,': 0.84; 'front.': 0.84; 'lena': 0.84; 'received:23': 0.84; 'canonical': 0.91 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1438539561772:2847616549 X-MC-Ingress-Time: 1438539561772 Date: Sun, 2 Aug 2015 13:19:16 -0500 From: Tim Chase To: python-list@python.org Subject: Re: Most pythonic way of rotating a circular list to a canonical point In-Reply-To: References: X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1438544186 news.xs4all.nl 2966 [2001:888:2000:d::a6]:38030 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94900 On 2015-08-01 13:34, Lukas Barth wrote: > I have a list of numbers that I treat as "circular", i.e. [1,2,3] > and [2,3,1] should be the same. Now I want to rotate these to a > well defined status, so that I can can compare them. > > If all elements are unique, the solution is easy: find the minimum > element, find its index, then use mylist[:index] + mylist[index:], > i.e. the minimum element will always be at the beginning. > > But say I have [0,1,0,2,0,3]. I can in fact guarantee that no > *pair* will appear twice in that list, i.e. I could search for the > minimum, if that is unique go on as above, otherwise find *all* > positions of the minimum, see which is followed by the smallest > element, and then rotate that position to the front. Well, you can pull the minimum (or maximum) of all the rotations to get the "canonical" version: def canonical(lst): """return the "canonical" representation of a list""" return min( lst if i == 0 else (lst[i:] + lst[:i]) for i in range(len(lst)) ) which you can determine, then hash once, and store. It's not a cheap operation, but once you've determined the canonical/hash version, then equality-testing becomes an O(1) test if comparing hashes, or O(N) if comparing lists rather than an O(N*2) brute-force test (which could be lower depending on the commonality). def circular_compare1(a, b): if a == b: return True return any(a == (b[i:] + b[:i]) for i in range(1, len(b))) def circular_compare2(a, b): lena = len(a) if lena != len(b): return False return any( all(a[i] == b[(i + offset) % lena] for i in range(lena)) for offset in range(lena) ) for fn in (circular_compare1, circular_compare2): for (a, b), expected in ( (([1,2,3], [1,2,3]), True), # the same (([1,2,3], [3,1,2]), True), # simple rotation (([1,2,3], [1,2,3,4]), False), # mismatched lengths (([0,1,0,2,0,3], [0,2,0,3,0,1]), True), # repeated elements, simple rotation ): result = bool(fn(a, b)) if result == expected: print "PASS %s %r/%r=%s, not %s" % ( fn.__name__, a, b, result, expected ) else: print "FAIL %s %r/%r=%s, not %s" % ( fn.__name__, a, b, result, expected ) ca = canonical(a) cb = canonical(b) print " Canonical A:", ca print " Canonical B:", cb print " Equal:", (ca == cb) -tim