I need to write a function called compress(S)
that takes a binary string S
of length less than or equal to 64 as input and returns another binary string as output. The output binary string should be a run-length encoding of the input string.
Run-length coding represents an image by a sequence (called a "run-length sequence") of 8-bit bytes:
The first bit of each byte represents the bit that will appear next in the image, either 0 or 1.
The final seven bits contain the number in binary of those bits that appear consecutively at the current location in the image.
>>>compress( 64*'0' )
'01000000'
>>> compress( '11111' )
'10000101'
>>> Stripes = '0'*16 + '1'*16 + '0'*16 + '1'*16
>>> compress(Stripes)
'00010000100100000001000010010000'
First Run Length Encoding, define it as-
def rle(input_string):
""" takes input as string and checks repeating bit
return repeating bit with their counts.
"""
count = 1
prev = ''
lst = []
for character in input_string:
if character != prev:
if prev:
entry = (prev,count)
lst.append(entry)
#print lst
count = 1
prev = character
else:
count += 1
else:
entry = (character,count)
lst.append(entry)
return lst
Produce list of tuples.
Input: print rle('1111010')
Output: [('1', 4), ('0', 1), ('1', 1), ('0', 1)]
----------------------------------
Now use this list to make dictionary with binary conversion of repeating counts and format it to 7 bits long. And finally add the respective key and values of the dict so that total digit remain 8 bits.
def new_dict(S):
""" input of rle(S) i.e., tuples of bit and repeating counts
output dict as bit as key and value as counts with binary conversion.
"""
dict=rle(S)
new_dict = []
temp = []
for k,v in dict:
temp = k + "%07d" % int(bin(v)[2:])
new_dict.append(temp)
return new_dict
input: print new_dict('1111010')
output: ['10000100', '00000001', '10000001', '00000001']
Now compress this binary string with the compress(S) function.
def compress(S):
""" takes a binary string S of length less than or equal to 64 as input and returns another binary string as output.
"""
l = new_dict(S)
return''.join(str(elem) for elem in l)
print compress('1111010')
print compress( 64*'0' )
output: '10000100000000011000000100000001'
output: '01000000'
print compress( '11111' )
output: '10000101'
Stripes = '0'*16 + '1'*16 + '0'*16 + '1'*16
print compress(Stripes)
output: '00010000100100000001000010010000'