optimization question
Terry Reedy
tjreedy at udel.edu
Mon Aug 12 01:16:44 EDT 2002
"Andrew Koenig" <ark at research.att.com> wrote in message
news:yu99d6sp2gel.fsf at europa.research.att.com...
> >> def eqsub(s, i, j, t):
> >> return (len(t) == j-i) and s[i:j] == t
> >>
> >> which avoids building the substrings unless necessary.
> >>
>
> Bernard> Wouldn't the following avoid it altogether?
>
> Bernard> return (len(t) == j-i) and (s.find(t,i) != -1)
>
> Yes, it looks like it would. On the other hand, I might want
> to generalize the technique to sequences other than strings...
def subseqmatch(seq, i, j, sub):
if len(sub) != j-i: return False
for item in sub:
if item != seq[i]: return False
i += 1
return True
print subseqmatch('0123456789', 3, 6, '345'),\
subseqmatch([0,1,2,3,4,5,6], 3, 6, [3,4,5]),\
subseqmatch((0,1,2,3,4,5,6), 3, 6, [3,4,6]) # miss
1 1 0
Terry J. Reedy
More information about the Python-list
mailing list