encode decode代码

刷题遇到的小程序,拿来分享

import sys
def encode(list_of_integers):
    # print(list_of_integers)
    result = ""
    for item in list_of_integers:
        bin_item = bin(item)[2:]
        for e in bin_item:
            result = result + str(e)*2
        result = result +'0'
    result = result[:-1]
    return int(result,2)


def decode(integer):
    temp = ""
    result = list()
    str1 = str(bin(integer)[2 :])
    i = 0
    if len(str1)==1:return None
    while(True):
        try:
            if(str1[i] == str1[i+1]):
                temp = temp + str1[i]
                i = i + 2
            elif str1[i]!=str1[i+1] and str1[i]=='0':
                result.append(temp)
                temp=""
                i = i + 1
            else:
                return None
        except IndexError:
            return None

        if i >= len(str1):
            break
    result.append(temp)
    
    return [int(e,2) for e in result]


# We assume that user input is valid. No need to check
# for validity, nor to take action in case it is invalid.
print('Input either a strictly positive integer')
the_input = eval(input('or a nonempty list of strictly positive integers: '))
if type(the_input) is int:
    print('  In base 2,', the_input, 'reads as', bin(the_input)[2 :])
    decoding = decode(the_input)
    if decoding is None:
        print('Incorrect encoding!')
    else:
        print('  It encodes: ', decode(the_input))
else:
    print('  In base 2,', the_input, 'reads as',
          f'[{", ".join(bin(e)[2: ] for e in the_input)}]'
         )
    print('  It is encoded by', encode(the_input))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值