【python】凯撒密码+纽卡斯伯爵的What Is Liquid

一、凯撒密码

题目:凯撒密码是古罗马凯撒大帝用来对军事情报进行加解密的算法,它采用了替换方法对信息中的每一个英文字符循环替换为字母表序列中该字符后面的第三个字符,

即,字母表的对应关系如下:

原文:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
密文:D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

对于原文字符P,其密文字符C满足如下条件:C=(P+3) mod 26

上述是凯撒密码的加密方法,解密方法反之,即:P=(C-3) mod 26

请编写一个程序,对输入字符串进行凯撒密码加密,直接输出结果,其中空格不用进行加密处理。
 

an1 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
in_an = input("")
out_an = ""
for j in in_an:
    if j==" ":
        out_an=out_an+" "
        continue
    i=an1.find(j)
    if(i > -1):
        out_an = out_an + an1[i+3]
print(out_an)

思想:安排一个数组,在这个数组里面找每个字母改变了相对位置后对应的字母符号,排列后再进行排列进行输出;在里面的an1如果仅仅是26位那么存在xyz是会报错-->超范围(IndexError: string index out of range)所以我们这里进行两倍的数组大小。

二、What Is Liquid

编程实现对纽卡斯伯爵的不朽名篇What Is Liquid的统计工作。这首诗(1)有多少个字符?(计入空格和换行符)(2)判断是否以All开头?(3)判断是否以That’s all, folks!结尾?(4)第一次和最后一次出现单词the的位置(偏移量)。(5)the出现的总次数?(6)判断诗中出现的所有字符是否都是字母和数字?
诗的内容:All that doth flow we cannot liquid name
Or else would fire and water be the same;
But that is liquid which is moist and wet
Fire that property can never get.
Then 'tis not cold that doth the fire put out
But 'tis the wet that makes it die, no doubt.

a = "All that doth flow we cannot liquid name  Or else would fire and water be the same;  But that is liquid which is moist and wet  Fire that property can never get.  Then 'tis not cold that doth the fire put out  But 'tis the wet that makes it die, no doubt."
print(len(a))
print(a.find("the"))
print(a.rfind("the"))
print(a.count("the"))
print(a.isdigit())
print(a.isalpha())

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

K-Pioneer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值