Skip to content

Instantly share code, notes, and snippets.

@jckw
Created April 20, 2016 18:21
Show Gist options
  • Save jckw/cf32d961f05917d203bb322e84d5dc30 to your computer and use it in GitHub Desktop.
Save jckw/cf32d961f05917d203bb322e84d5dc30 to your computer and use it in GitHub Desktop.
Basic text RLE compression (despite increasing file size in most cases)
import sys
dotTxt = sys.argv[1]
textfile = open(dotTxt)
string = textfile.read()
compressed = []
noInRow = 1
for i in range(0, len(string)-1):
prevLetter = string[i]
nextLetter = string[i + 1]
if nextLetter == prevLetter:
noInRow += 1
else:
compressed.append([noInRow, prevLetter])
noInRow = 1
compressed.append([noInRow, string[-1]])
finalStr = ""
for pair in compressed:
finalStr += ''.join(str(x) for x in pair)
print(finalStr)
print("*" * 50)
print("INITIAL :", len(string))
print("FINAL :", len(finalStr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment