Created
May 9, 2015 20:25
-
-
Save Achifaifa/0e4b25ea075cdb6c58bd to your computer and use it in GitHub Desktop.
python functions to encode and decode strings and files with rle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def encode(string): | |
counter=1 | |
previous=out="" | |
for ind,i in enumerate(string): | |
if i==previous: counter+=1; previous=i | |
if ind+1==len(string) or i!=previous: | |
if counter>2: | |
if ind+1==len(string): out+="%i%s"%(counter,i) | |
else: out+="%i%s"%(counter,previous) | |
else: | |
if ind+1==len(string): out+=(i*counter) | |
else: out+=(previous*counter) | |
counter=1;previous=i | |
return out | |
def decode(string): | |
counter="" | |
out="" | |
for i in string: | |
if i.isdigit():counter+=i | |
else: | |
if counter:out+=(int(counter)*i) | |
else:out+=i | |
counter="" | |
return out | |
def encodescroll(): | |
with open("./vscroll","r") as scrollin: | |
with open("./rsescroll","w+") as scrollout: | |
for line in scrollin: scrollout.write(encode(line.rstrip('\n'))+'\n') | |
def decodescroll(): | |
with open("./rsescroll","r") as scrollin: | |
with open("./finalscroll","w+") as scrollout: | |
for line in scrollin: scrollout.write(decode(line.rstrip('\n'))+'\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment