Skip to content

Instantly share code, notes, and snippets.

@rfb
Created January 16, 2022 21:29
Show Gist options
  • Save rfb/ab68855774c9dce879cc6406c7fd45fe to your computer and use it in GitHub Desktop.
Save rfb/ab68855774c9dce879cc6406c7fd45fe to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
ZERO_COUNT = 4
ONE_COUNT = 8
def ppm_scan(s):
count = 0
for bit in s:
if bit == "1":
if count > 0:
yield(count)
count = 0
elif bit =="0":
count = count + 1
else:
raise ValueError("got %s expects 0 or 1" % bit)
if count > 0:
yield(count)
def count_to_bits(counts):
for count in counts:
if count == ZERO_COUNT: yield(0)
elif count == ONE_COUNT: yield(1)
else: raise ValueError("got %s expects %0 or %0" % (count, ZERO_COUNT, ONE_COUNT))
for bit in count_to_bits(ppm_scan(sys.argv[1])):
sys.stdout.write(str(bit))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment