Skip to content

Instantly share code, notes, and snippets.

@MitI-7
Created December 30, 2015 09:08
Show Gist options
  • Save MitI-7/530ab93143375fb3ccce to your computer and use it in GitHub Desktop.
Save MitI-7/530ab93143375fb3ccce to your computer and use it in GitHub Desktop.
Run Length Encoding
from itertools import groupby
def encoding_rle(s):
return "".join((k + str(len(list(g)))) for k, g in groupby(s))
def decoding_rle(s):
return "".join(s[i] * int(s[i + 1]) for i in range(0, len(s), 2))
def main():
s = "AAAAABBBBBBBBBAAA"
e = encoding_rle(s)
d = decoding_rle(e)
print("s = {0}\nr = {1}\nd = {2}".format(s, e, d))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment