in Uncategorized

Solution for long repeat on checkio

I figured I’d share my solution for Long Repeat on Checkio. I got up this morning and had a few moments to solve. Did some research and found the groupby method from Itertools… there was an example online on how to use so I drew largely from that.

from itertools import groupby


def long_repeat(line):
    try:
        groups = groupby(line)
        result = [(k, sum(1 for _ in v)) for k, v in groups]
        occ = []
        for _ in result:
            k, v = _
            occ.append(v)
        return max(occ)
    except:
        return 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("") == 0
    assert long_repeat("abababaab") == 2
    print('"Run" is good. How is "Check"?')

Write a Comment

Comment