如前所述,运行是指连续重复值的序列。实现一个名为longest_run的Python函数,它接受一个数字列表并返回最长运行的长度。例如,在以下序列中:
2, 7, 4, 4, 2, 5, 2, 5, 10, 12, 5, 5, 5, 5, 6, 20, 1
最长运行的长度为4。然后,在主程序中,您的程序应该要求用户输入列表,然后调用longest_run函数并输出结果。
这是我尝试的代码,但它只返回1,我不知道为什么。在此问题中,无法导入任何模块。
这是我尝试的代码,但它只返回1,我不知道为什么。在此问题中,无法导入任何模块。
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