語法:
1
|
Elias Delta Encoding(X) = Elias Gamma encoding ( 1 + floor(log2(X)) + Binary representation of X without MSB. |
1、分步實施
首先,在為 Elias Delta
編碼編寫代碼之前,我們將實現 Elias delta
編碼。
第1步:
-
從數學庫導入
log
、floor
函數以執行對數運算。 -
從用戶獲取輸入 k 以在
Elias Gamma
中進行編碼。 -
使用數學模塊中的
floor
和log
函數,找到1+floor(log2(X)
并將其存儲在變量 N 中。 -
使用
(N-1)*'0'+'1'
找到 N 的一元編碼,它為我們提供了一個二進制字符串,其中最低有效位為 '1',其余最高有效位為 N-1 個'0'。
示例: 某些值的 Elias Gamma
編碼
1
2
3
4
5
6
|
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) |
第2步:
-
創建一個函數,該函數接受輸入 X 并給出結果作為 X 的二進制表示,沒有
MSB
。 -
使用
“{0:b}”.format(k)
找到 k 的二進制等效項并將其存儲在名為binary
的變量中。
-
前綴零僅指定應使用
format()
的哪個參數來填充 {}。 - b 指定參數應轉換為二進制形式。
-
返回字符串
binary[1:]
,它是 X 的二進制表示,沒有MSB
。
示例: 不帶 MSB
的二進制表示
1
2
3
4
|
def Binary_Representation_Without_MSB(x): binary = "{0:b}" . format ( int (x)) binary_without_MSB = binary[ 1 :] return binary_without_MSB |
現在我們要為 Elias Delta Encoding
編寫代碼
第3步:
-
從用戶獲取輸入 k 以在
Elias Delta
中進行編碼。 -
使用數學模塊中的
floor
和log
函數,找到1+floor(log2(k)
。 -
將
1+floor(log2(k)
的結果傳遞給Elias Gamma
編碼函數。
示例:某些值的 Elias Delta
編碼
1
2
3
4
5
6
7
8
|
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)) |
第4步:
-
得到不帶
MSB
的 k 的Elias Gamma
編碼和二進制表示的結果 - 連接兩個結果并在控制臺上打印它們
為某些整數值生成 Elias Delta
編碼的完整代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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)) |
輸出:
00100110
到此這篇關于Python 中 Elias Delta 編碼詳情的文章就介紹到這了,更多相關Python 中 Elias Delta 編碼內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://juejin.cn/post/7029486355738525733