2018北理工python语言程序设计期末测试编程题

第一道题:凯撒密码B

 

sr1="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
str1=input("")
str2=""
for i in str1:
    if i==" ":
        str2+=" "
    elif i >= 'a' and i <= 'z':
        j=sr1.find(i)
        str2=str2+sr1[(j+3)%26]
    elif i >= 'A' and i <= 'Z':
        j=sr1.find(i)
        str2=str2+sr1[(j+3)%26 + 26]
    else:
        str2 = str2 + i


print(str2)




第二道题:水仙花数B

ls = []
for num1 in range(100, 1000):
    a = num1 % 10
    b = (num1//10) % 10
    c = num1 // 100
    if (a**3 + b**3 + c**3) == num1:
        ls.append(num1)


for i in ls[0:-1]:
    print("{},".format(i),end='')


print(ls[-1])


第三道题:输入名字,我想对你说
 

name = input()
words = input()


print("{},我想对你说,{}".format(name,words))

 

第四道题:垂直输出

str1 = input()


for i in str1:
    print(i)

第五道题:哈姆雷特词频统计

def getText():
    txt = open("hamlet.txt", "r").read()
    txt = txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
        txt = txt.replace(ch, " ")   #将文本中特殊字符替换为空格
    return txt
 
hamletTxt = getText()
words  = hamletTxt.split()
counts = {}
for word in words:           
    counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True) 
for i in range(10):
    word, count = items[i]
    print ("{0:<10},{1:>5}".format(word, count))

 

你可能感兴趣的:(慕课网课程解答)