Compress the String!

Sort by

recency

|

816 Discussions

|

  • + 0 comments
    from itertools import groupby 
    for N, group in groupby(input()): 
        print((len(list(group)), int(N)), end=' ')
    
  • + 0 comments
    from itertools import groupby
    s = input()
    group = groupby(s,int)
    
    for key, grp in group:
        print((len(list(grp)),key),end=" ")
    
  • + 0 comments

    My solution:-

    from itertools import groupby
    S=input()
    print(*[(len(tuple(elements)), key) for key, elements in groupby(S, int)])
    
  • + 0 comments

    Sometimes you need to try to solve the problem with your onw logic. and here is mine, , whats do you think champs ?

    s = '1222311'
    count = 1
    for i in range(1, len(s)):
        # if i+1 < len(s):
        # print(i, i-1)
        if s[i] == s[i-1]:
            count += 1
        else: 
            print(f"({count}, {s[i-1]})", end=' ')
            count = 1
    print(f"({count}, {s[-1]})")
    
  • + 0 comments

    from itertools import groupby

    Reading input

    s = input()

    Using groupby to group consecutive identical characters

    result = [(len(list(group)), int(key)) for key, group in groupby(s)]

    Printing the result with the required format

    print(*result)