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
Post a Comment