Pick out the element repete continuously most time.
# Question 8: Longest Repetition
# Define a procedure, longest_repetition, that takes as input a
# list, and returns the element in the list that has the most
# consecutive repetitions. If there are multiple elements that
# have the same number of longest repetitions, the result should
# be the one that appears first. If the input list is empty,
# it should return None.
def longest_repetition(ls):
if ls == []:
return None
appearance = 1
max_number = 1
e = ls[0]
for i in range(1, len(ls)):
if ls[i] == ls[i-1]:
appearance += 1
if appearance > max_number:
max_number = appearance
e = ls[i]
else:
appearance = 1
return e
print longest_repetition([2, 2, 3, 3, 3])
# Incorrect. Your procedure did not return
# `3` for the input
# `[2, 2, 3, 3, 3]`.
#For example,
# print longest_repetition([1, 2, 2, 3, 3, 3, 2, 2, 1])
# # 3
#
# print longest_repetition(['a', 'b', 'b', 'b', 'c', 'd', 'd', 'd'])
# # b
#
# print longest_repetition([1,2,3,4,5])
# # 1
#
# print longest_repetition([])
# None