#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Consecutive Max in a list?


    Hey, does anyone have any idea of how to write a function that has a list of numbers as input (i.e def getConsecutiveMax(alist)) and iterates through and returns the value that is repeated the most consecutive times? For example getConsecutiveMax([2,3,3,4,4,4,3,3]) would return 4...For values that have the same max-consecutive repetition, the one that occurs first would be returned so [1,1,2,2] would have 1 returned...I'm thinking about using some type of for loop but can't figure out how to keep track of x-amounts of counts going simultaneously, and then returning the value that those counts refer to. Thank you!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    You might construct a function from
    Code:
    >>> A = [2,3,3,4,4,4,3,3]  # the list
    >>> set(A)  # the numbers in the list
    set([2, 3, 4])
    >>> A.count(2)
    1
    >>> A.count(3)
    4
    >>> A.count(4)
    3
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    I thought about doing something like that but in that scenario, 3 would have the greatest count but I would want 4 returned since it's the value that appears the most in one consecutive sequence.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    I figured it out, so if anyone reading the thread was intrigued, here's what I got:
    Code:
    def findConsecutiveMax(alist):
        numbers = alist
        currentCount = 0
        maxCount = 0
        last = numbers[0]
        for char in numbers:
            if char == last:
                currentCount = currentCount + 1
                last = char
                if currentCount > maxCount:
                    repeated = char
            else:
                if currentCount > maxCount:
                    maxCount = currentCount
                currentCount = 1
                last = char
        return repeated
    Thanks again for your response, you are always so helpful!
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    I didn't read the problem carefully---sorry.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo