Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Long Repeat by nash8114
import itertools
def long_repeat(line: str) -> int:
"""
length the longest substring that consists of the same char
"""
# itertools.groupby will group an iterator by consecutive value and return
# a list of tuples with a value (character in this case) and a grouper
# object. This grouper object contains the consecutive characters of the string
# To get the number of consecutive characters from the grouper object
# I convert it to a list and measure the length of that.
# The max function will find the highest number returned. The "default=0" is
# present to support empty strings.
return max(map(lambda g: len(list(g[1])), itertools.groupby(line)), default=0)
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"?')
April 10, 2020