头歌python之数值数据表示(一) ※

ec1115f0478740edbcea85e8873e6b3b.png

 #第一题

def dec2bin_Int(dec):

    binum = ''

    # 请在此添加代码,补全函数dec2bin_Int

    #-----------Begin----------

    while dec != 0:

        r = dec % 2

        if r == 1:

            binum = binum + "1"

        if r == 0:

            binum = binum + "0"

        dec = dec // 2

 

    #------------End-----------

    return binum[::-1]

#第二题

def dec2bin_Point(dec, length):

    binum = ''

    # 请在此添加代码,补全函数dec2bin_Point

    #-----------Begin----------

    len_ = 1

    while dec != 0 and len_ <= length:

        dec = dec * 2

        if dec > 1:

            binum = binum + "1"

            dec = dec - 1

        else:

            binum = binum + "0"

        len_ += 1

    #------------End-----------

    return '0.'+binum

#第三题

def bin2oh(binum, oh):

    result = ''

    if oh == 'o':

        # 请在此添加代码,补全函数bin2oh

        #-----------Begin----------

        length = len(binum)

        while length % 3 != 0:

            binum =  '0' + binum

            length = len(binum)

        for i in range(0,length,3):                 

            i_result = int(binum[i])*4 + int(binum[i+1])*2+ int(binum[i+2])

            result = result + str(i_result)

            

        #------------End-----------

        return result

    elif oh == 'h':

        # 请在此添加代码,补全函数bin2oh

        # -----------Begin----------

 

        length = len(binum)

        while length % 4 != 0:

            binum =  '0' + binum

            length = len(binum)

        for i in range(0,length,4):          

            i_result = int(binum[i])*8 + int(binum[i+1])*4+ int(binum[i+2])*2 + int(binum[i+3])

            dic = {10:"A",11:"B",12:"C",13:"D",14:"E",15:"F"}

            if i_result >= 10:

                i_result = dic[i_result]

            result = result + str(i_result)

 

 

 

        #------------End-----------

        return result

ac8fc1e362cc4ea4a7b9bf39ae80bab6.png

from random import *

 

def makechange(num):

    changes = {}

    numint = int(num)

    numdec = round(num - numint, 2)

    # 请在此添加代码,补全函数makechange

    #-----------Begin----------

    for i in [50,20,10,5,2,1]:

        if numint >= i:

            changes[i] = numint // i

            numint = numint % i

    numdec = numdec * 100

    for i in [0.5,0.2,0.1,0.05,0.02,0.01]:

        if numdec >= i * 100:

            changes[i] = int(numdec // (i * 100))

            numdec = numdec % (i * 100)

 

 

    #------------End-----------

    return changes

 

 

if __name__ == '__main__':

    seed(0)

    for i in range(10):

        num = round(random() * 100, 2)

        print(sorted(makechange(num).items(), key=lambda item: item[0], reverse=True))

42d5167953dc487f935a20db628db306.png

import math

 

def encryptMessage(key, message):

    # 请在此添加代码,补全函数encryptMessage

    #-----------Begin----------

    length = len(message)

    while length % key != 0:

        length += 1

    message_key = ""

    for i in range(key):

        for j in range(i,len(message),key):

            message_key += message[j]

    return message_key

    #------------End-----------

 

def decryptMessage(key, message):

    # 请在此添加代码,补全函数decryptMessage

    #-----------Begin----------

    message_de = ''

    n = len(message) / key

    if n > len(message) // key:

        n += 1

    n = int(n)

    m = n * key - len(message)

    single = key - m

    totle = 0

    for i in range(n):

        count = 0

        j = i

        while count < key and totle < len(message):

            if count > single:

                j -= 1

            message_de += message[j]

            totle += 1

            j += n

            count += 1

    return message_de

 

    #------------End-----------

 

 

if __name__ == '__main__':

    messages = ["Behind every successful man there's a lot of unsuccessful years.",

                'Common sense is not so common.',

                'There are no secrets to success.It is the result of preparation, hard work, and learning from failure.',

                'All things are difficult before they are easy.']

    for message in messages:

        for key in range(8, 10):

            encrytext = encryptMessage(key, message)

            print(encrytext)

            print(decryptMessage(key, encrytext))

 

 

 

  • 9
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值