stephen stephen - 3 months ago 12
Python Question

Finding the length of the longest repeating 1s in a string

I was wondering how one can find the length of the longest repeating 1s in a string of 1s and 0s in python, taking into account the empty string. like

'1011110111111'
would return
6
and
''
would return
0
.

A past post suggested using regex,

max([len(i) for i in re.compile('(1+1)').findall(count)])


but it doesn't count the case for one single 1 and the empty string.

Answer

Just split the string using '0' as separator and find the longest item in the list:

s = '1011110111111'
result = len(max(s.split('0')))
Comments