Skip to content

Instantly share code, notes, and snippets.

@akolybelnikov
Created November 10, 2020 17:12
Show Gist options
  • Save akolybelnikov/913c08b71e7b557f3d856a8c8a2720bd to your computer and use it in GitHub Desktop.
Save akolybelnikov/913c08b71e7b557f3d856a8c8a2720bd to your computer and use it in GitHub Desktop.
def compress(raw: str) -> bytes:
bArr = raw.encode('utf-8')
print([b for b in bArr])
if len(bArr) == 1:
return bytes([1, bArr[0]])
rList = []
l = 0
while l < len(bArr):
count = 1
n = l
while n < len(bArr) - 1 and bArr[n] == bArr[n+1]:
count += 1
n += 1
l += count
rList.append(bytes([count, bArr[l-1]]))
return b''.join(rList)
b = compress("abbb")
print(b)
u = compress("\N{GRINNING FACE} \N{SHAMROCK}")
print(u)
v = compress("e")
print(v)
s = compress("\N{SHAMROCK}")
print(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment