python-checkio/Home/long-repeat.py

29 lines
860 B
Python

def long_repeat(line: str) -> int:
longest = 0
"""
length the longest substring that consists of the same char
"""
current = None
current_counter = 0
for char in line:
if char != current:
if current_counter > longest:
longest = current_counter
current = char
current_counter = 1
else:
current_counter += 1
if current_counter > longest:
longest = current_counter
return longest
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"?')