-
Notifications
You must be signed in to change notification settings - Fork 1
/
golomb_coding.py
76 lines (68 loc) · 1.74 KB
/
golomb_coding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import math
n=int(input("enter the value of n"))
m=int(input("enter the value of m"))
def golomb_coding(n,m):
a=math.floor(math.log(m,2))
b=math.ceil(math.log(m,2))
p=math.log(m,2)
q=math.floor(n/m)
r=n-q*m
def power(k):
global u
if(k%2==0):
if(k/2==1):
u=1
else:
power(k/2)
else:
u=0
power(m)
def decimalToBinary(n):
global j
j=bin(n).replace("0b", "")
def unary():
if(q==0):
print("golomb_code:",end="")
print('1',end="")
else:
print("golomb_code:",end="")
for i in range(q):
print('0',end="")
print('1',end="")
unary()
if(u==1):
decimalToBinary(r)
global j
if(len(j)<p):
for v in range(int(p-len(j))):
r='0'+j
j=r
print(r)
elif(len(j)==p):
r=j
print(r)
z=pow(2,math.ceil(p))-m
if(u==0):
for f in range(z):
if(r==f):
decimalToBinary(r)
if(len(j)<a):
for v in range(int(a-len(j))):
r='0'+j
j=r
print(r)
elif(len(j)==a):
r=j
print(r)
for f in range(z,m):
if(r==f):
decimalToBinary(r+z)
if(len(j)<b):
for v in range(int(b-len(j))):
r='0'+j
j=r
print(r)
elif(len(j)==b):
r=j
print(r)
golomb_coding(n,m)