{"id":26890,"date":"2022-03-31T02:19:42","date_gmt":"2022-03-30T20:49:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=26890"},"modified":"2022-03-31T02:19:42","modified_gmt":"2022-03-30T20:49:42","slug":"python-elias-gamma-encoding","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-elias-gamma-encoding\/","title":{"rendered":"Python Elias Gamma Encoding"},"content":{"rendered":"
Peter Elias created the Elias Gamma Encoding, which is used to encode a sequence of positive numbers. Let’s examine how we can use Python to encode a positive integer using this encoding technique.<\/p>\n
What is Elias Gamma Encoding?<\/strong><\/p>\n The Elias gamma code is a universal code for encoding a series of positive integers. It’s particularly beneficial when the upper bound of an integer can’t be determined in\u00a0advance.<\/p>\n Assume the number to be encoded is N. The steps for Elias Gamma Encoding of N are as follows:<\/p>\n Example<\/strong><\/p>\n Let the Number = 19<\/p>\n As (19 = 2^4 + 3), the greatest feasible value of\u00a0 ‘x’ <\/strong>in this scenario is 4<\/strong>.<\/p>\n Unary encoding:\u00a0<\/strong> By performing unary encoding we get 00001.<\/p>\n Then we need to get the four-digit binary representation of 3, which is 0011.<\/strong><\/p>\n It should be added to 00001. <\/strong><\/p>\n As a result, our encoded string is 000010011.<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n NOTE:<\/strong> It should be noted that Elias Gamma Encoding is useful when the upper bound of integers cannot be determined.<\/p>\n","protected":false},"excerpt":{"rendered":" Peter Elias created the Elias Gamma Encoding, which is used to encode a sequence of positive numbers. Let’s examine how we can use Python to encode a positive integer using this encoding technique. What is Elias Gamma Encoding? The Elias gamma code is a universal code for encoding a series of positive integers. It’s particularly …<\/p>\n\n
Elias Gamma Encoding in Python<\/h2>\n
\n
# Import parse from urllib module using the import keyword \r\nimport math\r\n# Perform the Unary encoding by using the lambda function\r\nunary_encoding = lambda num: num * '0' + '1'\r\n# Perform the binary encoding by using the lambda function\r\nbinary_encoding = lambda num, l = 1:(\"{0:0%db}\" % l).format(num)\r\n# Create a function say eliasEncoding() which returns the Elias Encoded string for the given number\r\ndef eliasEncoding(num):\r\n # Check if the number is equal to 0 using the if conditional statement\r\n if(num==0):\r\n # If it is true then return '0'\r\n return '0'\r\n \r\n # Get the base-2 log value of the given number using the log() function and \r\n # Store it in a variable\r\n logval = int(math.log(num, 2))\r\n \r\n b = num - 2 ** logval\r\n # Pass the above log value to the unary_encoding() function and b, log value to the binary_encoding() function\r\n # and sum up both the unary and binary encoded values \r\n rslt = unary_encoding(logval) + binary_encoding(b, logval)\r\n # Return the above result\r\n return rslt\r\n\r\n# Pass some random number to the above created eliasEncoding() function \r\n# to perform the Elias Encoding operation and print the result\r\nprint(\"The Elias Encoding of the given number{10}:\")\r\nprint(eliasEncoding(10))<\/pre>\n
The Elias Encoding of the given number{10}:\r\n0001010<\/pre>\n