From 9f074d796a375310199aca505102d43a06e3c7f7 Mon Sep 17 00:00:00 2001 From: Rishabh Singh Chauhan <143814425+Rsc2414@users.noreply.github.com> Date: Sun, 20 Jul 2025 22:37:19 +0530 Subject: [PATCH] Update 005. Compress the String!.py --- .../06. Itertools/005. Compress the String!.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Python/06. Itertools/005. Compress the String!.py b/Python/06. Itertools/005. Compress the String!.py index aa7c5de..094e5c6 100644 --- a/Python/06. Itertools/005. Compress the String!.py +++ b/Python/06. Itertools/005. Compress the String!.py @@ -1,8 +1,16 @@ -# Problem: https://www.hackerrank.com/challenges/compress-the-string/problem -# Score: 20 +from itertools import groupby +# Take input string +s = input() -from itertools import groupby +# Initialize an empty list to store the result +result = [] + +# Group the string using groupby +for digit, group in groupby(s): + count = len(list(group)) # Count how many times the digit repeats + result.append((count, int(digit))) # Add tuple to result list +# Print the result, unpacked +print(*result) -print(*[(len(list(c)), int(x)) for x, c in groupby(input())])