Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.034 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; "(i'm": 0.16; 'benjamin': 0.16; 'both.': 0.16; 'equal.': 0.16; 'range(0,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'unequal,': 0.16; 'string': 0.17; 'wrote:': 0.17; "i've": 0.23; 'random': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'actual': 0.28; 'comparison': 0.29; "d'aprano": 0.29; 'steven': 0.29; "i'm": 0.29; 'good.': 0.32; 'to:addr:python-list': 0.33; 'equal': 0.33; 'done': 0.34; 'subject:?': 0.35; 'received:org': 0.36; 'but': 0.36; 'depends': 0.36; 'useful': 0.36; 'ok,': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'range': 0.60; 'here': 0.65; 'frequency': 0.65; 'talking': 0.66; 'complexity': 0.84; 'maths': 0.84; 'oscar': 0.84; 'increases': 0.91; 'average': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Oscar Benjamin Subject: Re: Comparing strings from the back? Date: Fri, 7 Sep 2012 19:40:00 +0000 (UTC) References: <504564ba$0$29978$c3e8da3$5496439d@news.astraweb.com> <504761ef$0$29981$c3e8da3$5496439d@news.astraweb.com> <50477cbb$0$29981$c3e8da3$5496439d@news.astraweb.com> <50485fca$0$29977$c3e8da3$5496439d@news.astraweb.com> <504972d1$0$29981$c3e8da3$5496439d@news.astraweb.com> X-Gmane-NNTP-Posting-Host: cpc1-aztw8-0-0-cust1455.18-1.cable.virginmedia.com User-Agent: slrn/pre1.0.0-18 (Linux) 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347046816 news.xs4all.nl 6912 [2001:888:2000:d::a6]:59519 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28708 On 2012-09-07, Oscar Benjamin wrote: > On 2012-09-07, Steven D'Aprano wrote: >> > > Since string comparison is only useful if the strings can be equal or unequal, > the average case depends on how often they are equal/unequal as well as the > average complexity of both. For random strings the frequency of equal strings > decreases very fast as N increases so that the comparison of random strings is > O(1). > >> >> (I'm talking about the average here -- the actual number of comparisons >> can range all the way up to N, but the average is <= 2.) >> >> If I've done the maths right, the exact value for the average is: >> >> ((M-1)*sum( (N-i)*M**i for i in range(0, N) ) + N)/(M**N) > > I'm not sure where the extra N comes from --------^ but otherwise good. Ok, I see it's for the case where they're equal. Oscar