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

后端 未结 6 2303
南方客
南方客 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:14

    def getSublists(L,n):
        outL=[]
        for i in range(0,len(L)-n+1):
            outL.append(L[i:i+n])
        return outL
    
    
    def longestRun(L):
        for n in range(len(L), 0, -1):
            temp=getSublists(L,n)
            for subL in temp:
                if subL==sorted(subL):
                    return len(subL)
    

提交回复
热议问题