When not to use an RE -- an example

John Machin sjmachin at lexicon.net
Tue Apr 22 04:27:36 EDT 2003


"Chris" <password.april03.20.netman at spamgourmet.com> wrote in message news:<BC1pa.23530$5f4.9990 at twister.nyroc.rr.com>...
> What's wrong with:
> 
>     def findrepeat(s):  return len(s) > 1 and not filter(lambda x: x !=
> s[0], s)
> 
> Chris
> 

What's wrong with it? It rates almost as high on the ugliness scale as
the crazy RE that I posted. A guess on efficiency: It's slow.
Rationale: My minimalist proposition (repeated below) takes two
substrings and then compares them, giving up as soon as a non-repeat
is detected [most likely case in my application]. Yours takes no
substrings, but has the overhead of calling the lambda function for
each & every character in the string -- it doesn't terminate early.

> > def repeats2(s):
> >    return len(s) > 1 and s[1:] == s[:-1]




More information about the Python-list mailing list