Python语言程序设计(嵩天老师)期末考试—第四部分全部

今天下午抽空把最后一部分的考试考完了,总体来说,没有前面的两章困难,很多都是基础问题,或者是书上的示例程序,进行了一些小的调整和修改,因为代码长度都不是很长,就把所有的程序放到一篇博客里。

1.凯撒密码B

示例程序在课程配套教材《Python语言程序设计基础》83页。稍作修改

PassInit = input()
for ind in PassInit:
    if (ord('a') <= ord(ind) <= ord('z')):
        PassKaisa = chr(ord('a')+(ord(ind)-ord('a')+3)%26)
        print(PassKaisa,end='')
    elif  (ord('A') <= ord(ind) <= ord('Z')):
        PassKaisa = chr(ord('A')+(ord(ind)-ord('A')+3)%26)
        print(PassKaisa,end='')
    else:
        PassKaisa = ind
        print(PassKaisa,end='')
        continue

2. 三位水仙花数计算

除了计算个位、十位和百位,以及对应输出格式之外,也没有什么难点

NumofSXH = []
for ind in range(100,1000):
    A = ind//100
    B = (ind//10%10)
    C = ind%10
    if pow(A,3) + pow(B,3) + pow(C,3) == ind:
        NumofSXH.append(ind)
    else:
        continue
LenSXH = len(NumofSXH)
for ind1 in range(LenSXH):
    if ind1 < (LenSXH-1):
        print('{},'.format(NumofSXH[ind1]),end='')
    else:
        print('{}'.format(NumofSXH[ind1]))

3. 说句心里话

你们都想说什么呢~~

a = input()
b = input()
str1 = a + ',我想对你说,' + b
print(str1)

4. 字符串垂直输出

我在想是否应该可以一行代码搞定的,我可能写复杂了

a = input()
for ind in a:
    print(ind)

5. 词频统计《哈姆雷特》

这道题相对来说是代码量比较大的题,自己写有一丢丢的难度,但是与课程上给的实例完全相同,只有一些输出格式上的变化,因此代码有问题的,可以去看看老师讲课的视频,以及书本172页代码。

def getText():
    txt = open('hamlet.txt','r').read()
    txt = txt.lower()
    for ch in '!"#$%&()*+,-./:;<>=?@[]\\^_{}|`~':
        txt = txt.replace(ch," ")
    return txt

if __name__ == '__main__':
    content = getText()
    words = content.split()
    counts = {}
    for ind in words:
        counts[ind] = counts.get(ind,0) + 1
    items = list(counts.items())
    items.sort(key=lambda x:x[1], reverse=True)
    for ind1 in range(10):
        word, times = items[ind1]
        print('{:<10},{:>5}'.format(word,times))

至此,嵩天老师的课程结束了。本学期在MOOC上修了两门Python的课,分别是《Pythopn语言程序设计》以及《用Python玩转数据》应该都可以拿到优秀证书,很开心。

现在在看深度学习大牛辛顿老师在Coursera上的《Neural Network in Machine Learning》。希望学成归来可以再上传一些代码。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值