longest sequence

Bengt Richter bokr at oz.net
Mon Feb 17 17:19:57 EST 2003


On Mon, 17 Feb 2003 11:44:11 -0600, "Mark McEahern" <mark at mceahern.com> wrote:

>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?
>

 >>> def longest(seq): return reduce(max, map(len, seq))
 ...
 >>> aSeq = '1111', '222'
 >>> longest(aSeq)
 4

Regards,
Bengt Richter




More information about the Python-list mailing list