Created
July 27, 2013 18:38
-
-
Save wadoon/6095831 to your computer and use it in GitHub Desktop.
* Elias Gamma & Delta Codes
* Golomb code
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
#!/usr/bin/python | |
from math import log,ceil | |
log2 = lambda x: log(x,2) | |
def binary(x,l=1): | |
fmt = '{0:0%db}' % l | |
return fmt.format(x) | |
def unary(x): | |
return x*'1'+'0' | |
def elias_generic(lencoding, x): | |
if x == 0: return '0' | |
l = 1+int(log2(x)) | |
a = x - 2**(int(log2(x))) | |
k = int(log2(x)) | |
return lencoding(l) + binary(a,k) | |
def golomb(b, x): | |
q = int((x) / b) | |
r = int((x) % b) | |
l = int(ceil(log2(b))) | |
#print q,r,l | |
return unary(q) + binary(r, l) | |
def elias_gamma(x): | |
return elias_generic(unary, x) | |
def elias_delta(x): | |
return elias_generic(elias_gamma,x) | |
for i in range(11): | |
print "%5d: %-10s : %-10s : %-10s" %(i, elias_gamma(i),elias_delta(i), golomb(3,i)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment