Advertisement
MaxDvc

RLE compression/decompression

Dec 9th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. def RLEcompression(string):
  2. #@param:
  3. #   string: str;
  4. #   return: str;
  5.     newString = ""
  6.     count = 1
  7.     for i in range(0,len(string)):
  8.         if i+1<len(string):
  9.             if string[i]==string[i+1]:
  10.                 count += 1
  11.             else:
  12.                 if count <=2:
  13.                     newString = newString + count*string[i]
  14.                 else:
  15.                     newString = newString + str(count) + string[i]
  16.                 count = 1
  17.         else:
  18.             if count <=2:
  19.                 newString = newString + count*string[i]
  20.             else:
  21.                 newString = newString + str(count) + string[i]
  22.                 count = 1
  23.     return newString
  24. #-------------------------------------------------------------------------------
  25. def RLEdecompression(string):
  26. #@param:
  27. #   string: str, RLE compressed string;
  28. #   return: str;
  29.     newString = ""
  30.     for i in range(0,len(string)):
  31.             if string[i].isdigit() and i+1<len(string):
  32.                 newString = newString + int(string[i])*string[i+1]
  33.             else:
  34.                 newString = newString + string[i]
  35.     return newString
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement