Created
February 6, 2013 23:38
-
-
Save hackjoy/4726924 to your computer and use it in GitHub Desktop.
Calculates and returns the element in a list that has repeated the most times in succession.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def longest_repetition(list): | |
best_length = 0 | |
best_element = None | |
current_length = 0 | |
current_element = None | |
for e in list: | |
if e != current_element: | |
current_element = e | |
current_length = 1 | |
else: | |
current_length += 1 | |
if current_length > best_length: | |
best_length = current_length | |
best_element = current_element | |
return best_element | |
# print longest_repetition([1, 2, 2, 3, 3, 3, 2, 2, 1]) | |
# 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment