Python 中 Elias Delta 編碼詳情
語法:
Elias Delta Encoding(X)= Elias Gamma encoding (1+floor(log2(X)) + Binary representation of X without MSB.
1、分步實(shí)施
首先,在為 Elias Delta 編碼編寫代碼之前,我們將實(shí)現(xiàn) Elias delta 編碼。
第1步:
- 從數(shù)學(xué)庫導(dǎo)入
log、floor函數(shù)以執(zhí)行對(duì)數(shù)運(yùn)算。 - 從用戶獲取輸入 k 以在
Elias Gamma中進(jìn)行編碼。 - 使用數(shù)學(xué)模塊中的
floor和log函數(shù),找到1+floor(log2(X)并將其存儲(chǔ)在變量 N 中。 - 使用
(N-1)*'0'+'1'找到 N 的一元編碼,它為我們提供了一個(gè)二進(jìn)制字符串,其中最低有效位為 '1',其余最高有效位為 N-1 個(gè)'0'。
示例: 某些值的 Elias Gamma 編碼
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步:
- 創(chuàng)建一個(gè)函數(shù),該函數(shù)接受輸入 X 并給出結(jié)果作為 X 的二進(jìn)制表示,沒有
MSB。 - 使用
“{0:b}”.format(k)找到 k 的二進(jìn)制等效項(xiàng)并將其存儲(chǔ)在名為binary的變量中。
- 前綴零僅指定應(yīng)使用
format()的哪個(gè)參數(shù)來填充 {}。 - b 指定參數(shù)應(yīng)轉(zhuǎn)換為二進(jìn)制形式。
- 返回字符串
binary[1:],它是 X 的二進(jìn)制表示,沒有MSB。
示例: 不帶 MSB 的二進(jìn)制表示
def Binary_Representation_Without_MSB(x):
binary = "{0:b}".format(int(x))
binary_without_MSB = binary[1:]
return binary_without_MSB
現(xiàn)在我們要為 Elias Delta Encoding 編寫代碼
第3步:
- 從用戶獲取輸入 k 以在
Elias Delta中進(jìn)行編碼。 - 使用數(shù)學(xué)模塊中的
floor和log函數(shù),找到1+floor(log2(k)。 - 將
1+floor(log2(k)的結(jié)果傳遞給Elias Gamma編碼函數(shù)。
示例:某些值的 Elias Delta 編碼
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編碼和二進(jìn)制表示的結(jié)果 - 連接兩個(gè)結(jié)果并在控制臺(tái)上打印它們
為某些整數(shù)值生成 Elias Delta 編碼的完整代碼
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
到此這篇關(guān)于Python 中 Elias Delta 編碼詳情的文章就介紹到這了,更多相關(guān)Python 中 Elias Delta 編碼內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信