问:运行是一系列相邻的重复值.给定一个列表,编写一个函数来确定最长运行的长度.例如,对于序列[1,2,5,5,3,1,2,4,3,2,2,2,3,3,5,5,6,3,1],最长跑是4.
我遇到了麻烦,我写了一个代码,发现最长的运行由数字'2'组成,但还没有得到运行的长度为4.
这是我到目前为止的代码(我已经注释掉了我正在研究的部分,但是没有注意它):
# longestrun.py
# A function to determine the length of the longest run
# A run is a sequence of adjacent repeated values.
def longestrun(myList):
result = None
prev = None
size = 0
max_size = 0
for i in myList:
if i == prev:
size += 1
if size > max_size:
result = i
max_size = size
else:
size = 0
prev = i
return result
def main():
print("This program finds the length of the longest …Run Code Online (Sandbox Code Playgroud)