Created
July 31, 2019 21:58
-
-
Save MagnetonBora/f71f9c675ec6a0c81580a684ec50bc35 to your computer and use it in GitHub Desktop.
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
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