Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Python/06. Itertools/005. Compress the String!.py
Original file line number Diff line number Diff line change
@@ -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())])