How to Find the Maximum Consecutive Occurrences of a Number in Python

How to find the maximum consecutive occurrences of a number in python

the only change you need is

if val >= num_times:

your code is comparing the length of the current group with the last key.

this returns the last item with maximal consecutive occurrence (if there were three times 5 later in the list this would be selected).

count the highest consecutive occurrence of a string in Python

This snippet loops over consecutive concatenated "hello"s. It first checks if there's a "hello" in the string, then "hellohello", etc, until it fails. The longest sequence that was there is the max number of consecutive "hello"s.

How to print the maximum consecutive occurrences of 1's in python 2.7?

You should do something like this :

get_num_ones = re.findall(r"1+", y)
print "Maximum Nb of consecutive 1's =", len(max(get_num_ones, key=len))

What I'm doing here is get all the possible consecutive one's in the binary string, and then I pick the string with longest length.

Find Max consecutive occurrences on a String Python

You can try:

import re

def max_occur(s, words):
repeats = [list(map(lambda x: len(x) // len(word), re.findall(rf'(?:{word})+', s))) for word in words]
return [1 if max(rep, default=0) == 1 else sum(r for r in rep if r > 1) for rep in repeats]

print(max_occur('ababcbabc', ["ab", "babc", "bca"])) # [2, 2, 0]
print(max_occur('aaabaa', ['a', 'b', 'c'])) # [5, 1, 0]
print(max_occur('aaabaabb', ['a', 'b', 'c'])) # [5, 2, 0]

The regex here detects repeats of word, and then divide the length of each matched string by the length of the word (len(x) // len(word)), which yields the number of repeats. The rest of the code processes this result using somewhat complicated logic; if the occurrence is at max 1 (i.e., only singletons), then just spit out 1. Otherwise sum over non-singleton repeats.

How to count Consecutive Maximum Occurrence of a word in an item of a list in Python

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')

Find maximum length of consecutive repeated numbers in a list

You can use itertools.

In [8]: import itertools

In [9]: z = [(x[0], len(list(x[1]))) for x in itertools.groupby(a)]

In [10]: z
Out[10]: [(1, 2), (2, 3), (3, 1)]

Tuples are in (item, count) format. If there are multiple runs of a given number, this will group them accordingly as well. See below.

In [11]: a = [1,1,1,1,1,2,2,2,2,2,1,1,1,3,3]

In [12]: z = [(x[0], len(list(x[1]))) for x in itertools.groupby(a)]

In [13]: z
Out[13]: [(1, 5), (2, 5), (1, 3), (3, 2)]

Getting the max value isn't that hard from here.

In [15]: max(z, key=lambda x:x[1])[1]
Out[15]: 5

How would you find the maximum number a string has been repeated consecutively in a list?

You can do this using itertools.groupby, which groups consecutive elements. By providing no key function, it groups equal elements.

We need to use sum(1 for _ in v) to get the length of the run (since it is an iterable rather than a list, we can't use len), and max to get the biggest length.

from itertools import groupby

def longest_run_of(lst, element):
return max(sum(1 for _ in v) for k, v in groupby(lst) if k == element)

Example:

>>> longest_run_of(lst, 'ABC')
3
>>> longest_run_of(lst, 'XYZ')
1


Related Topics



Leave a reply



Submit