无法将编码信息传递给第二个函数。 我知道当返回变量时数据基本上是None。 但我正在寻找一种解决方法。
string = 'WWWWBBWWWWBWWWBBBBWWWWWWBBWWWWW'
# Encoding algroithm.
def runLengthEncoding(data):
runCount = 0
i = 0
data = data + '5'
encoded = ''
for j in range(len(data)):
if data[i] == data[j]:
runCount += 1
else:
encoded += str(runCount)+ '' + data[i]
i = j
runCount = 1
return encoded
# Decoding algorithm.
def runLengthDecoding(encoded):
k = 0
decoded = ''
for l in range(len(encoded), 2):
if encoded[k]:
if encoded[l]:
decoded += int(encoded[k]) * encoded[l]
k += 2
return decoded
print('\t\t\t***Run Length Encoding***\n')
print('Uncompressed:', string)
print('Encoded: ', runLengthEncoding(string))
print('\n\t\t\t***Run Length Decoding***\n')
print('Encoded: ', runLengthEncoding(string))
# Can't use the return value.
print('Decoded: ', runLengthDecoding(runLengthEncoding(string)))
答案 0 :(得分:0)
问题不在于您无法使用返回值,问题是您的解码功能实际上并未向您的字符串添加任何内容而只返回"&#34 ;。用这个无bug版本替换你的解码功能,它将起作用:
def runLengthDecoding(encoded):
decoded = ''
for i in range(0, len(encoded), 2):
decoded += int(encoded[i])*encoded[i+1]
return decoded