Compress the String! Hacker Rank Solution – Queslers

Problem: Compress the String! Hacker Rank Solution

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.

Input Format :

A single line of input consisting of the string S.

Output Format :

A single line of output consisting of the modified string.

Constraints :

All the characters of S denote integers between 0 and 9.

  • 1 <= | S | <= 10^4

Sample Input :

1222311

Sample Output :

(1, 1) (3, 2) (1, 3) (2, 1)

Explanation :

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.

Compress the String! Hacker Rank Solution in python 2

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))

Compress the String! Hacker Rank Solution in pyhton 3

from itertools import groupby
for key, group in groupby(input()):
   print('({}, {})'.format(len(list(group)), key), end=" ")

Compress the String! Hacker Rank Solution in pypy

# 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()))

Compress the String! Hacker Rank Solution in pypy 3

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=" ")
Compress the String! Hacker Rank Solution Review:

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

Conclusion:

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

Nested Lists Hacker Rank Solution

Lists Hacker Rank Solution

Leave a Reply

Your email address will not be published. Required fields are marked *