Skip to content Skip to sidebar Skip to footer

Longest Sequence Of Consecutive Duplicates In A Python List

As mentioned, a run is a sequence of consecutive repeated values. Implement a Python function called longest_run that takes a list of numbers and returns the length of the longest

Solution 1:

The biggest mistake of your code is to set bucket=[] (which is a list) and later to an integer.

Also, you need to store the longest sequence and the current sequence length (initialized to 1) and the last seen value, so more variables than you're storing.

Each time value is the same as before, increase counter. If it's different, reset counter after having checked if it's not greater than the max. In the end perform the max test again just in case the longest sequence is in the end (classic mistake)

like this:

seq = [2, 7, 4, 4, 2, 5, 2, 5, 10, 12, 5, 5, 5, 5, 6, 20, 1]

result=1
max_result=0
last_seen=seq[0]

for v in seq[1:]:
    if v==last_seen:
        result += 1
    else:
        if result > max_result:
            max_result = result
        last_seen = v
        result = 1

# just in case the longest sequence would be at the end of your list...
if result > max_result:
    max_result = result

print(max_result)

When you're finally allowed to use python batteries, use itertools.groupby and compute the max of the sequence lengths:

max(sum(1 for x in v) for _,v in itertools.groupby(seq))

Solution 2:

You get 1 since when your loop is in it's final iteration: bucket = 20 and i = 1 which means bucket != i so the loop enters the else clause and assigns count = 1 exits and the function returns count which is 1.

Suggestions:

1) When you encounter a bug like this try running through the code/logic manually - it helps.

2) For this question specifically - whenever a run ends you forget the last run length, think about how you can "remember" the longest run.


Solution 3:

So you're trying to find the longest run of the same number in your list? Because it's kinda confusing, what you were trying to code.

You should keep two versions of count: maxCount (the one which you're gonna return) and actualCount (the one you're gonna increment), iterate through the list and compare number with the next one. If it's the same actualCount += 1 if not, actualCount = 0 at the end of every iteration, compare maxCount and actualCount and if actualCount is bigger than maxCount = actualCount.

def longest_run(aList):

    maxCount = 1
    actualCount = 1

    for i in range(len(aList)-1):
        if aList[i] == aList[i+1]:
            actualCount += 1
        else:
            actualCount = 1
        if actualCount > maxCount:
            maxCount = actualCount

    return(maxCount)

Solution 4:

You can use the following method:(Number of repetitions of each number)

mylist = [2, 7, 4, 4, 2, 5, 2, 5, 10, 12, 5, 5, 5, 5, 6, 20, 1]
my_dict = {i:mylist.count(i) for i in mylist}
mylist = list(dict.fromkeys(mylist))
R_list=[]
for i in mylist:
    print("%s repeated %s" %(i,my_dict[i]))
    R_list = R_list+[my_dict[i]]
print(R_list)
print(max(R_list))  

Post a Comment for "Longest Sequence Of Consecutive Duplicates In A Python List"