Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
groupby and len solution in Clear category for Long Repeat by casilda
def long_repeat(line: str) -> int:
"""
length the longest substring that consists of the same char
"""
from itertools import groupby
if line == '': return 0
# make list of all substrings with same consecutive character with groupby:
substrings = [list(g) for k, g in groupby(line)]
# Return lengthiest list - Don't need to worry about order since 'substrings' kept original order
# and max will return first item if there are two with the same value
return max([len(x) for x in substrings])
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert long_repeat('sdsffffse') == 4, "First"
assert long_repeat('ddvvrwwwrggg') == 3, "Second"
assert long_repeat('abababaab') == 2, "Third"
assert long_repeat('') == 0, "Empty"
print('"Run" is good. How is "Check"?')
Sept. 19, 2019
Comments: