Skip to content

Instantly share code, notes, and snippets.

@HemantNegi
Created December 12, 2018 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HemantNegi/027f8aabd96107d32c66e96a41ad346f to your computer and use it in GitHub Desktop.
Save HemantNegi/027f8aabd96107d32c66e96a41ad346f to your computer and use it in GitHub Desktop.
Data encoding by difference
def delta_encode(array):
first = False
pre = None
diff = None
res = []
for i in array:
if not first:
res.append(i)
first = True
pre = i
else:
diff = i - pre
if not -127 <= diff <= 127:
res.append(-128)
res.append(diff)
pre = i
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment