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
itertools.groupby
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).
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
1) Converting the number to a string
2) Using
itertools.groupby
to group each subsequent repeating digit3) Concatenating each grouped digit with the size of its group
4) Concatenating the whole thing to one string
(I interpreted the question so that the expected output for 151 is 111511 and not 2115).