“This is the 12th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

🌊 author’s home page: Haichong 🌊 Author Profile: πŸ₯‡HDZ core group member, πŸ† full stack quality creator, 🌊 has been ranked top ten in the weekly list of C station for a second year. Fan welfare: Send four books to fans every week (each one has), and send various small gifts every month (gold boring enamelware cup, pillow, mouse pad, mug, etc.)

In this article, we will implement Elias Delta encoding using Python.

Grammar:

Elias Delta Encoding(X)= Elias Gamma encoding (1+floor(log2(X)) + Binary representation of X without MSB.
Copy the code

Step by step implementation

First, we will implement the Elias Delta coding before writing the code for it.

Step 1:

  • Import the log, floor functions from the math library to perform logarithmic operations.
  • Get input K from the user to encode in Elias Gamma.
  • Using the floor and log functions in the math module, find 1+floor(log2(X) and store it in the variable N.
  • Use (n-1)*’0’+’1′ to find the unary encoding of N, which gives us a binary string with the least significant bits ‘1’ and the remaining most significant bits ‘0’.

Example: Elias Gamma encoding for some values

def EliasGammaEncode(k) :
	if (k == 0) :return '0'
	N = 1 + floor(log(k, 2))
	Unary = (N-1) *'0'+'1'
	return Unary + Binary_Representation_Without_MSB(k)
Copy the code

Step 2:

  • Create a function that takes input X and gives the result as a binary representation of X, without MSB.

  • Use “{0:b}”. Format (k) to find the binary equivalent of k and store it ina variable named binary.

    • The prefix zero simply specifies which argument of format() should be used to populate {}.
    • B specifies that the parameters should be converted to binary form.
  • Return the string binary[1:], which is the binary representation of X without MSB.

Example: binary representation without MSB

def Binary_Representation_Without_MSB(x) :
	binary = "{0:b}".format(int(x))
	binary_without_MSB = binary[1:]
	return binary_without_MSB
Copy the code

Now we will write the code for Elias Delta Encoding

Step 3:

  • Get input K from the user to encode in Elias Delta.
  • Using the floor and log functions in the math module, find 1+floor(log2(k).
  • The result of 1+floor(log2(k) is passed to the Elias Gamma coding function.

Example: Elias Delta encoding for some values

def EliasDeltaEncode(x) :
	Gamma = EliasGammaEncode(1 + floor(log(k, 2)))
	binary_without_MSB = Binary_Representation_Without_MSB(k)
	return Gamma+binary_without_MSB


k = int(input('Enter a number to encode in Elias Delta: '))
print(EliasDeltaEncode(k))
Copy the code

Step 4:

  • Results of Elias Gamma encoding and binary representation of k without MSB
  • Connect the two results and print them on the console

Generates the complete Elias Delta code for some integer values

from math import log
from math import floor

def Binary_Representation_Without_MSB(x) :
	binary = "{0:b}".format(int(x))
	binary_without_MSB = binary[1:]
	return binary_without_MSB

def EliasGammaEncode(k) :
	if (k == 0) :return '0'
	N = 1 + floor(log(k, 2))
	Unary = (N-1) *'0'+'1'
	return Unary + Binary_Representation_Without_MSB(k)

def EliasDeltaEncode(x) :
	Gamma = EliasGammaEncode(1 + floor(log(k, 2)))
	binary_without_MSB = Binary_Representation_Without_MSB(k)
	return Gamma+binary_without_MSB

k = 14
print(EliasDeltaEncode(k))
Copy the code

Output:

00100110
Copy the code

Write it at the end

The author is determined to build a fishing site with 100 small games, update progress: 40/100

I’ve been writing a technical blog for a long time, mostly through Nuggets, and this is my article on Elias Delta coding in Python. I like to share technology and happiness through articles. You can visit my blog at juejin.cn/user/204034… For more information. Hope you like it! 😊

πŸ’Œ welcomes your comments and suggestions in the comments section! πŸ’Œ