RLE to ASCII (decompression)
By James
This post demonstrates a simple and easy way to convert compressed RLE back into ASCII using Python.
Note how string splicing is achieved:
Example:
Given s="Python is great"
>>>s[2:3] - start a position 2, end at position 3
"y"
>>>s[3:5] - start at postion 3 and end at position 5
"th"
>>>s[3:] - starting at position 3 to the end of the string
"thon is great"
def RLEtoASCII(x):
n=0
c= x
a=0
while len(c)>0: # loop through the length of the RLE compressed string
a=int(c[:2]) # takes the first two characters
c=c[2:] # get whats left of the string
b=c[:1] # get the next character
c=c[1:] # get whats left of the string
for i in range(0,a,1):
print(b, end="")
print('\r')
Function RLEtoASCII takes one parameter which is the compressed RLE string
EXAMPLE: "04a05b06s17f05c"
feel free to copy and paste snippets of the code and make it your own (^?^)