Compress the String!

Sort by

recency

|

859 Discussions

|

  • + 0 comments
    from itertools import groupby
    result = [f"({len(list(group))}, {char})" for char,group in groupby(input())]
    print (" ".join(result))
    
  • + 0 comments
    from itertools import groupby
    s = input()
    for char, grouped_iter in groupby(s):
        print(f"({len(list(grouped_iter))}, {char})", end = " ")
    
  • + 0 comments

    from itertools import groupby

    input_value = input()

    group_result = groupby(input_value)

    for key,value in group_result:

    value = sorted(list(value))
    print(f"({int(len(value))}, {int(key)})", end=" ")
    
  • + 0 comments

    Core Python code (Without Built-in function)- We will appreciate itertools module some other time :

    string= input()
    
    lst= [element for element in string]
    
    count= 1
    
    prev= lst[0]
    
    for i in range(1, len(lst)):
        if lst[i] == prev:
            count = count + 1
    
        else:
            print(f"({count}, {prev})", end= " ")
            prev= lst[i]
            count = 1
    
    print(f"({count}, {prev})")
    
  • + 0 comments
    import itertools
    
    print(" ".join(f"({len(list(g))}, {k})" for k, g in itertools.groupby(input())))