<\/span><\/h2>\n\n\n\nIn this task, we would like for you to appreciate the usefulness of the groupby()<\/em> function of itertools<\/em> .<\/p>\n\n\n\nYou are given a string S<\/em><\/strong>. Suppose a character ‘c<\/em><\/strong>‘ occurs consecutively X<\/strong><\/em> times in the string. Replace these consecutive occurrences of the character ‘c<\/strong><\/em>‘ with (X<\/em>, c<\/em>)<\/strong> in the string.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nA single line of input consisting of the string S<\/em><\/strong>.<\/p>\n\n\n\n<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nA single line of output consisting of the modified string.<\/p>\n\n\n\n
<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- All the characters of S<\/strong><\/em> denote integers between 0<\/strong> and 9<\/strong>.<\/li>
- 1 <= |S<\/em>| <= 104<\/sup><\/strong><\/li><\/ul>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\n1222311<\/code><\/pre>\n\n\n\nSample Output <\/strong><\/p>\n\n\n\n(1, 1) (3, 2) (1, 3) (2, 1)<\/code><\/pre>\n\n\n\nExplanation<\/strong><\/p>\n\n\n\nFirst, the character 1<\/strong> occurs only once. It is replaced by (1 , 1)<\/strong>. Then the character 2<\/strong> occurs three times, and it is replaced by (3, 2)<\/strong> and so on.<\/p>\n\n\n\nAlso, note the single space within each compression and between the compressions.<\/p>\n\n\n\n
<\/span>Solution- Compress the String! in Python <\/strong><\/span><\/h2>\n\n\n\n# Enter your code here. Read input from STDIN. Print output to STDOUT\nfrom itertools import groupby\n\nfor k, c in groupby(input()):\n print(\"(%d, %d)\" % (len(list(c)), int(k)), end=' ')<\/pre>\n\n\n\n