I'm trying to implement a code that will count the longest run of a word in an item of a list
for_example = ['smaplakidfsmapsmapsmapuarebeautiful']
so in this example, it will be 3 because smap was repeated 3 times so is there is a code that does this task for me no matter what the word is.
EDIT: If you have a list of items, you can call the function like this:
[countMaxConsecutiveOccurences(item, 'smap') for item in items]
def countMaxConsecutiveOccurences(item, s):
i = 0
n = len(s)
current_count = 0
max_count = 0
while i < len(item):
if item[i:i+n] == s:
current_count += 1
max_count = max(max_count, current_count)
i += n
else:
i += 1
current_count = 0
return max_count
countMaxConsecutiveOccurences('smaplakidfsmapsmapsmapuarebeautiful', 'smap')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.