Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 17th, 2013, 07:48 PM
 rbmlmc
Registered User

Join Date: Feb 2013
Posts: 5
Time spent in forums: 56 m 11 sec
Reputation 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
February 17th, 2013, 08:28 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
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!

#3
February 17th, 2013, 08:32 PM
 rbmlmc
Registered User

Join Date: Feb 2013
Posts: 5
Time spent in forums: 56 m 11 sec
Reputation 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.

#4
February 17th, 2013, 08:44 PM
 rbmlmc
Registered User

Join Date: Feb 2013
Posts: 5
Time spent in forums: 56 m 11 sec
Reputation 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```

#5
February 17th, 2013, 08:51 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
I didn't read the problem carefully---sorry.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Consecutive Max in a list?