Compress the String!

Sort by

recency

|

873 Discussions

|

  • + 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=" ")
    
  • + 0 comments

    from itertools import groupby

    A = input()

    B =groupby(A)

    for i, k in B: print((len(list(k)), int(i)), end=" ")

  • + 0 comments
    from itertools import groupby
    
    if __name__ == "__main__":
        s = input()
        print(" ".join(f"({len(list(group))}, {key})" for key, group in groupby(s)))
    
  • + 0 comments
    import itertools
    string = input()
    groups = itertools.groupby(string)
    results = []
    for key, group in groups:
        count = 0
        for item in group:
            count += 1
        results.append((count, int(key)))
    
    print(*results)
    
  • + 0 comments
    from itertools import groupby
    s = input()
    for key, group in groupby(s):
        print((len(list(group)), int(key)), end=" ")