Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'broken': 0.03; '"""': 0.05; 'python3': 0.05; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'b):': 0.16; 'benjamin': 0.16; 'combinations': 0.16; 'equal.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'stumbling': 0.16; 'wrote:': 0.17; 'comparing': 0.17; '>>>': 0.18; 'header:User-Agent:1': 0.26; 'common': 0.26; 'header:X-Complaints- To:1': 0.28; 'decide': 0.28; 'tail': 0.29; 'character': 0.29; 'words': 0.29; 'function': 0.30; "skip:' 20": 0.32; '"")': 0.33; 'to:addr:python-list': 0.33; 'subject:?': 0.35; 'received:org': 0.36; 'but': 0.36; 'characters': 0.36; 'compare': 0.36; 'skip:* 60': 0.36; 'does': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'first': 0.61; 'more': 0.63; 'results': 0.65; '****': 0.65; 'frequency': 0.65; 'oscar': 0.84; 'otten': 0.84; 'average': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Comparing strings from the back? Date: Wed, 05 Sep 2012 17:45:55 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b17a.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346859964 news.xs4all.nl 6919 [2001:888:2000:d::a6]:52233 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28510 Oscar Benjamin wrote: > On 5 September 2012 10:48, Peter Otten <__peter__@web.de> wrote: >> def count_common(a, b): [sorry for seriously broken implementation] > This function will return 1 if the first character differs. It does not > count the number of common characters but rather the more relevant > quantity which is the number of comparisons required to decide if the > strings are equal. I remember stumbling over the non-zero frequency for len(word) but somehow plodded on with beautifying the broken results :( def count_common(a, b): """ >>> count_common("alpha", "beta") 0 >>> count_common("", "") 0 >>> count_common("alpha", "argument") 1 >>> count_common("alpha", "alpha") 5 >>> count_common("alpha", "alphx") 4 """ i = 0 for x, y in zip(a, b): if x != y: break i += 1 return i $ python3 reverse_compare2.py compare /usr/share/dict/words 499500 combinations average common chars (head) 0.0535 average common chars (tail) 0.322 comparing every pair in a sample of 1000 8-char words taken from '/usr/share/dict/words' head 0: 477371 ************************************************************ 1: 18310 ** 2: 3191 3: 523 4: 79 5: 15 6: 10 7: 1 tail 0: 385069 ************************************************************ 1: 78742 ************ 2: 26734 **** 3: 7462 * 4: 1297 5: 168 6: 22 7: 6