Skip to content

Instantly share code, notes, and snippets.

@MagnetonBora
Created July 31, 2019 21:58
Show Gist options
  • Save MagnetonBora/f71f9c675ec6a0c81580a684ec50bc35 to your computer and use it in GitHub Desktop.
Save MagnetonBora/f71f9c675ec6a0c81580a684ec50bc35 to your computer and use it in GitHub Desktop.
from collections import defaultdict
def encode(text):
result = []
freq = defaultdict(int)
for prev, current in zip(text[:-1], text[1:]):
freq[prev] += 1
if current != prev:
result.append((prev, freq[prev]))
freq[prev] = 0
result.append((prev, freq[prev] + 1))
return result
def decode(data):
return ''.join(freq * symbol for symbol, freq in data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment