#第一题
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
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))
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))
文章出处登录后可见!