Compress the String!

  • + 0 comments

    Logic without using itertools.groupby() as:-

    def groupby_custom(s):
        l_group = []
        l_key = []
        count = 1
    
        for ind, i in enumerate(s):
            if ind+1 < len(s) and s[ind+1] == s[ind]:
                count += 1
            else:
                l_group.append([i] * count)
                l_key.append(i)
                count = 1
    
        return zip(l_key, l_group)
    
    s = list(input())
    
    for k, group in groupby_custom(s):
        print(f"({len(list(group))}, {k})", end=" ")