We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Itertools
- Compress the String!
- Discussions
Compress the String!
Compress the String!
Sort by
recency
|
863 Discussions
|
Please Login in order to post a comment
from itertools import groupby
def run_length_encoding(s): encoded_parts = [] for key, group in groupby(s): count = len(list(group)) encoded_parts.append(f'({count}, {key})') return ' '.join(encoded_parts)
input_string = input() output_string = run_length_encoding(input_string) print(output_string)
My shortest solution…
A solution without
groupby()
…