MIT 6.00.1x 计算机科学和Python编程导论 Set 6

本文介绍了MIT 6.00.1x课程中的凯撒密码加密和解密问题。学生需要编写程序实现加密和解密,其中解密部分通过寻找最大化英语单词数的解码方法来确定最佳移位。此外,提供了辅助函数和伪代码以帮助完成解密任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Part1

Problem 1: Encryption

感谢 glhezjnucn 童鞋对本周问题的给力翻译 !
You’ll now write a program to encrypt plaintext into ciphertext using the Caesar cipher. 你现在来写一个函数将使用凯撒密码将明文转为密文。

def buildCoder(shift):
    """
    Returns a dict that can apply a Caesar cipher to a letter.
    The cipher is defined by the shift value. Ignores non-letter characters
    like punctuation, numbers, and spaces.

    shift: 0 <= int < 26
    returns: dict
    """
    ### TODO 
    ciphertext = {}
    uppercase_plaintext = [i for i in string.ascii_uppercase]
    lowercase_plaintext = [i for i in string.ascii_lowercase]
    for i in range(26):
        if i < (26 - shift):
            ciphertext[uppercase_plaintext[i]] = uppercase_plaintext[i+shift]
        else:
            ciphertext[uppercase_plaintext[i]] = uppercase_plaintext[i+shift-26]
    for i in range(26):
        if i < (26 - shift):
            ciphertext[lowercase_plaintext[i]] = lowercase_plaintext[i+shift]
        else:
            ciphertext[lowercase_plaintext[i]] = lowercase_plaintext[i+shift-26]
    return ciphertext

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值