Compress the String!

  • + 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})")