Compress the String! Hacker Rank

Solution : 

from itertools import groupby

A = input()

for key,count in groupby(A):
    output=((len(list(count)),int(key)))
    print(output,end=" ")

Explanation :

input 1222311

for key,count in groupby(A) generate
key = 1 2 3 1
count is list = ['1'] ['2','2','2'] ['3'] ['1','1']

output is created like without len function for list
output=((list(count),int(key)))
(['1'], 1) (['2', '2', '2'], 2) (['3'], 3) (['1', '1'], 1)


Comments

Popular posts from this blog

Mean, Var, and Std in numpy HackerRank

Min and Max in numpy HackerRank

Collections.deque() HackersRank Solution