Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Compression Run Length encoding

I am trying to learn about run length encoding and I found this challenge online that I cant do. It requires you to write a compression function called compression(strg) that takes a binary string strg of length 64 as input and returns another binary string as output. The output binary string should be a run-length encoding of the input string.

compression('1010101001010101101010100101010110101010010101011010101001010101')

'1010101001010101*4'

Here is what I have, but this does NOT find the pattern:

from itertools import *

def compression(strg):
    return [(len(list(group)),name) for name, group in groupby(strg)]

I need some help solving this.

like image 938
user1681664 Avatar asked Dec 04 '25 17:12

user1681664


1 Answers

I believe that you are conflating RLE with Lempel/Ziv sliding window compression.

RLE strictly works on repeated characters: WWWWWWWW => W8

LZ has a sliding window that will pick up patterns as you describe.

David MacKay's site has example compression codes in Python, including LZ


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!