Cam
Cam

Reputation: 49

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 run. For example in the sequence: 2, 7, 4, 4, 2, 5, 2, 5, 10, 12, 5, 5, 5, 5, 6, 20, 1 the longest run has length 4. Then, in the main, your program should ask the user to input the list, then it should call longest_run function, and print the result.

This is what I tried but it only returns 1 and I don't understand why. I can't import any modules for this question.

def longest_run(aList):
  '''(list)->int
  Returns length of the longest run
  Precondition: aList is a list of a len of at least 2 and elements of list are ints
  '''
  count=0
  bucket=[]
  for i in aList:
    if bucket==i:
        count=count+1
    else:
        bucket=i
        count=1
  return count

Upvotes: 2

Views: 11125

Answers (4)

Seyed_Ali_Mohtarami
Seyed_Ali_Mohtarami

Reputation: 1164

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

Upvotes: 0

Alessandro Petric
Alessandro Petric

Reputation: 33

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)

Upvotes: 0

Jean-François Fabre
Jean-François Fabre

Reputation: 140148

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

Upvotes: 6

Omzaks
Omzaks

Reputation: 41

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.

Upvotes: 0

Related Questions