HackerRank Compress the String! solution in python | python question 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, Check this out .

You are given a string . Suppose a character ‘‘ occurs consecutively  times in the string. Replace these consecutive occurrences of the character ‘‘ with  in the string.

For a better understanding of the problem, check the explanation.

Input Format

A single line of input consisting of the string .

Output Format

A single line of output consisting of the modified string.


All the characters of  denote integers between  and .

Sample Input


Sample Output

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


First, the character  occurs only once. It is replaced by . Then the character  occurs three times, and it is replaced by  and so on.

Also, note the single space within each compression and between the compressions.

In this Compress the string problem we need to develop a python program that can read a string as input and then we need to print the tuples containing the number of occurrence of integers on the output screen.

Problem solution in Python 2 programming.

from itertools import groupby
l=[(sum(1 for i in g),k) for k,g in groupby(s)]
print ' '.join(map(str,l))

Problem solution in Python 3 programming.

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

Problem solution in pypy programming.

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

Problem solution in pypy3 programming.

for c in s :
if c == prev_c:
if prev_c!='':
print((count+1,int(prev_c)), end=" ")
print((count+1,int(prev_c)),end=" ")

Leave a Reply

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science 10 Best Artificial Intelligence Software|artificial intelligence tools