DEV Community

suneelvarma
suneelvarma

Posted on

Problem in Python

input=111115
output=5115

how to solve this problem in python?

Top comments (1)

Collapse
 
orenovadia profile image
orenovadia

1) Converting the number to a string
2) Using itertools.groupby to group each subsequent repeating digit
3) Concatenating each grouped digit with the size of its group
4) Concatenating the whole thing to one string

In [16]: ''.join(str(len(list(group))) + digit for digit, group in itertools.groupby(str(111115)))                                                                           
Out[16]: '5115'

(I interpreted the question so that the expected output for 151 is 111511 and not 2115).