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
|
865 Discussions
|
Please Login in order to post a comment
from itertools import groupby
if name == 'main': S = str(input()) results = [(len(list(g)),int(k)) for k,g in groupby(S)] result_as_str = list(map(lambda result: str(result), results)) print(' '.join(result_as_str))
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)