字符串通过执行以下操作序列进行编码:
例如,下表显示了从字符串"HelloWorld"
到ASCII字符串"7210110810811187111114108100"
的转换。
字符
H e l l o W o r l d
ASCII值
72 101 108 108 111 87 111 114 108 100
然后反转ASCII字符串以获得编码的字符串"0018014111117811180180110127
“。
编码字符串中的字符在10-126范围内,其中包括特殊字符.
我需要编写一个函数,它必须是解码编码的字符串,和返回解码方式的列表。
我找不到解决这个问题的办法。任何帮助都是非常感谢的。
发布于 2018-04-11 09:20:28
solutions = []
currentSolution = ''
unprocessed = ''
def decode(s):
flipped = s[::-1]
global solutions
global unprocessed
global currentSolution
currentSolution = ''
unprocessed = flipped
_decode()
return solutions
def is_valid(split):
if split.startswith('0'):
return False
value = int(split)
if value < 10 or value > 126:
return False
return True
def _decode():
global unprocessed
global currentSolution
global solutions
if len(unprocessed) == 0:
solutions.append(currentSolution)
else:
possible_splits = list()
possible_splits.append(unprocessed[0:2])
possible_splits.append(unprocessed[0:3])
for split in possible_splits:
if is_valid(split):
decoded_character = chr(int(split))
currentSolution += decoded_character
unprocessed = unprocessed[len(split):]
_decode()
currentSolution = currentSolution[0: len(currentSolution) - 1]
unprocessed = split + unprocessed
def main():
final_solutions = decode('0018014111117811180180110127')
print(final_solutions)
if __name__ == '__main__':
main()
https://stackoverflow.com/questions/49745241
复制相似问题