Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this task, we would like for you to appreciate the usefulness of the groupby() function of itertools . To read more about this function .
You are given a string S. Suppose a character ‘c’ occurs consecutively X times in the string. Replace these consecutive occurrences of the character ‘c’ with (X,c)
in the string.
For a better understanding of the problem, check the explanation.
A single line of input consisting of the string S.
A single line of output consisting of the modified string.
All the characters of S denote integers between 0 and 9.
1222311
(1, 1) (3, 2) (1, 3) (2, 1)
First, the character 1 occurs only once. It is replaced by (1, 1). Then the character 2 occurs three times, and it is replaced by (3, 2) and so on.
Also, note the single space within each compression and between the compressions.
from itertools import groupby
s=map(int,list(raw_input()))
l=[(sum(1 for i in g),k) for k,g in groupby(s)]
print ' '.join(map(str,l))
from itertools import groupby
for key, group in groupby(input()):
print('({}, {})'.format(len(list(group)), key), end=" ")
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import groupby
print " ".join(str((len(list(k)),int(i))) for i,k in groupby(raw_input()))
s=input()
prev_c=''
count=0
Out=()
for c in s :
if c == prev_c:
count+=1
else:
if prev_c!='':
print((count+1,int(prev_c)), end=" ")
prev_c=c
count=0
print((count+1,int(prev_c)),end=" ")
In our experience, we suggest you solve this Compress the String! Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
Compress the String! problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Compress the String! Hacker Rank Solution
I hope this Compress the String! Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.
This Problem is intended for audiences of all experiences who are interested in learning Programming in a business context; there are no prerequisites.
Keep Learning!
More Hacker Rank Problem & Solutions >>
Staircase Hacker Rank Solution
A Very Big Sum Hacker Rank Solution
Diagonal Difference Hacker Rank Solution