longest sequence

Jp Calderone exarkun at intarweb.us
Mon Feb 17 14:15:25 EST 2003


On Mon, Feb 17, 2003 at 12:34:14PM -0600, Mark McEahern wrote:
> [gustavo]
> > Yay, optimizations time! :-)
> >
> > def longest2(*args):
> >    if args:
> >       decorated = [ (len(S),S) for S in args ]
> >       decorated.sort()
> >       return decorated[-1][1]
> >
> > This should be a bit quicker. :-)
> 
> Yup, my testing shows your decorate-sort-undecorate approach is twice as
> fast.  Is the trick here that sort() sorts sequences of tuples on the first
> element of each tuple in the sequence?

  Close.  More accurately, tuples have an ordering defined.  In a sense,
it's the same as the ordering for strings.  Compare the first element, if
one is less than the other, the answer is found, otherwise compare the
second element, and so on.

  Jp

--
"There is no reason for any individual to have a computer in their
home."
                -- Ken Olson, President of DEC, World Future Society
                   Convention, 1977
-- 
 up 9 days, 0:28, 5 users, load average: 0.14, 0.09, 0.08
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030217/ee7444b9/attachment.sig>


More information about the Python-list mailing list