Python String Compression Write Python Script Takes Long String Compresses Using Ever Comp Q37299416

Python string compression.

Write a python script that takes a long string and compressesit

using what ever compression algorithm you want. (Preferablybasic).

then un-compress it and see if your algorithim is lossless.


Answer


def compress(str1):

result = “”

cnt = 1
result += str1[0]

for i in range(len(str1)-1):
if(str1[i] == str1[i+1]):
cnt+=1
else:
if(cnt > 1):
#Ignore if no repeats
result += str(cnt)
result += string[i+1]
cnt = 1
#print last one
if(cnt > 1):
result += str(cnt)
return result
  

def decompress(string):
  
str=””;
  
for i in range(len(string)):
if(i+1<len(string)):
if(string[i+1].isdigit()):
for j in range(int(string[i+1])):
str=str+string[i]
  
  
return str
  

string =”aabbcc”
print(“originl string :”,string)
compressed=compress(string)
print(“compressed string”,compressed)
decompress=decompress(compressed)
print(“compressed string”,decompress)
#print(str)

e Python String Compression. Wii GGIDEIGeestorGeeks l Acompute 0 (1) YouTube compression-Create compre: x Chttps://ide.geeksf

e Python String Compression. Wii GGIDEIGeestorGeeks l Acompute 0

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.