Python: A program to find the LENGTH of the longest run in a given list?

后端 未结 6 2308
南方客
南方客 2020-12-03 12:59

Q: A run is a sequence of adjacent repeated values. Given a list, write a function to determine the length of the longest run. For example, for the sequence [1, 2, 5, 5, 3,

6条回答
  •  情书的邮戳
    2020-12-03 13:27

    As an update to David Robinson's answer, it is now (Python 3.4) possible to return 0 on an empty sequence (instead of raising ValueError):

    import itertools
    max((sum(1 for _ in l) for n, l in itertools.groupby(lst)), default=0)
    

提交回复
热议问题