Compress the String!

Sort by

recency

|

857 Discussions

|

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

    Here is HackerRank Compress the String! in python solution - https://programmingoneonone.com/hackerrank-compress-the-string-solution-in-python.html

  • + 0 comments

    import itertools

    if name == 'main': S = input()

    result = [(len(list(group)), int(key)) for key, group in itertools.groupby(S)]    
    
    
    for item in result:
        print(item , end=' ')