longest sequence

sismex01 at hebmex.com sismex01 at hebmex.com
Mon Feb 17 12:47:51 EST 2003


> From: Mark McEahern [mailto:mark at mceahern.com]
> Sent: Monday, February 17, 2003 11:44 AM
> 
> How do you detetermine the longest sequence?  Naive use of 
> max doesn't work
> (for this):
> 
>   >>> max('1111', '222')
>   '222'
> 
> so I came up with this:
> 
>   def longest(*args):
>       sorted = list(args)
>       sorted.sort(lambda x, y: cmp(len(x), len(y)))
>       return sorted[-1]
> 
> Comments?
>

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. :-)

-gustavo





More information about the Python-list mailing list