Python: program for finding the LENGTH of the longest run in a given list?

Q: A run is a sequence of adjacent repeating values. Based on the list, write a function to determine the length of the longest run. For example, for the sequence [1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 6, 3, 1], the longest run is 4 .

I had problems with this, I wrote a code that believes that the longest mileage consists of the number "2", but has not yet received a mileage that is 4.

Here is my code so far (I commented on the part that I worked on, but do not pay attention to it):

# longestrun.py
#   A function to determine the length of the longest run
#   A run is a sequence of adjacent repeated values.

def longestrun(myList):
    result = None
    prev = None
    size = 0
    max_size = 0


    for i in myList:
        if i == prev:
            size += 1
            if size > max_size:
                result = i
                max_size = size
        else:
            size = 0
        prev = i
    return result



def main():
    print("This program finds the length of the longest run within a given list.")
    print("A run is a sequence of adjacent repeated values.")

    myString = input("Please enter a list of objects (numbers, words, etc.) separated by
    commas: ")
    myList = myString.split(',')

    longest_run = longestrun(myList)

    print(">>>", longest_run, "<<<")

main()

Help me please!!!: (((

+4
source share
5 answers

, itertools.groupby:

import itertools
max(sum(1 for _ in l) for n, l in itertools.groupby(lst))
+8

, itertools import.

a=[1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 6, 3, 1]

def longestrun(myList):
    result = None
    prev = None
    size = 0
    max_size = 0


    for i in myList:
        if i == prev:
            print (i)
            size += 1
            if size > max_size:
                print ('*******  '+ str(max_size))
                max_size = size 
        else:
            size = 0
        prev = i
    print (max_size+1)    
    return max_size+1


longestrun(a)
0

Another way to do this:

def longestrun(myList):
    sett = set()
    size = 1
    for ind, elm in enumerate(myList):
        if ind > 0:
            if elm == myList[ind - 1]:
                size += 1
            else:
                sett.update([size])
                size = 1
    sett.update([size])
    return max(sett)

myList = [1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 6, 3, 1]
print longestrun(myList)
0
source
def longestrun(myList):
    size = 1
    max_size = 0
    for i in range(len(myList)-1):
        if myList[i+1] = myList[i]:
            size += 1
        else: 
            size = 1
        if max_size<size:
            max_size = size
    return size 

Extract .split () from myList to main (), and it will be good for you to handle this.

0
source
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)
0
source

Source: https://habr.com/ru/post/1530355/


All Articles