Created
April 20, 2016 18:21
-
-
Save jckw/cf32d961f05917d203bb322e84d5dc30 to your computer and use it in GitHub Desktop.
Basic text RLE compression (despite increasing file size in most cases)
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
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