【发布时间】:2014-11-07 19:46:19
【问题描述】:
我有以下序列:'TATAAAAAAATGACA',我希望 Python 打印出包含最连续重复的字母......在这种情况下,这将是 7 'A's。仅使用for、if、len、range 和一个计数变量,您将如何做到这一点?
【问题讨论】:
-
这不是代码编写服务。您尝试过什么,到底有什么问题?
我有以下序列:'TATAAAAAAATGACA',我希望 Python 打印出包含最连续重复的字母......在这种情况下,这将是 7 'A's。仅使用for、if、len、range 和一个计数变量,您将如何做到这一点?
【问题讨论】:
def consecutiveLetters(word):
currentMaxCount = 1
maxChar = ''
tempCount = 1
for i in range(0, len(word) - 2):
if(word[i] == word[i+1]):
tempCount += 1
if tempCount > currentMaxCount:
currentMaxCount = tempCount
maxChar = word[i]
else:
currentMaxCount = 1
return [maxChar,tempCount]
result = consecutiveLetters("TATAAAAAAATGACA")
print("The max consecutive letter is ", result[0] , " that appears ", result[1], " times.")
这将打印:The max consecutive letter is A that appears 7 times.
【讨论】: