每日codingame小游戏练习[2021.4.01](python入门学习)

每日codingame小游戏练习[2021.4.01](python入门学习),第1张

概述题目描述InCaesarCodeyoutakeeveryletterofthegiventextandmovethemforwardbytheintegerkinthealphabet.(Attention:Afterzcomesa)Example:Normaltext:“hello”k=2h->je->gl->nl->no->qEncryptedtext:“jgnnq” 题目描述

In Caesar Code you take every letter of the given text and move them forward by the integer k in the Alphabet. (Attention: After z comes a)

Example:
normal text: “hello”
k = 2

h -> j
e -> g
l -> n
l -> n
o -> q

Encrypted text: “jgnnq”

Caesar Code is easy to break. Let’s try something a little harder.
Your task is Now to encrypt a given text t with a substitution, but Now k increases for each character (not only letter) by 1. For the first character k = 0, for the second k = 1, for the third k = 2 …

Leave everything that is not a letter (number, punctuation mark…) as it is.

eg:

inputOutput
aaaaaaabcdef
题目分析

题目有点长,直接看例子比较好。是一道有关凯撒密码变种,位移字母的算法。

解题代码

因为我的代码,没调试完成,直接分析俄罗斯老哥的代码:

for i,c in enumerate(input()):x=65+32*c.islower();print(end=(c,chr((ord(c)-x+i)%26+x))[c.isAlpha()])

把代码简化后:

for i,c in enumerate(input()): #提取下标    x=65+32*c.islower() #判断大小写    print((c, chr((ord(c)-x+i)%26+x)) [c.isAlpha()],end= '')     #c.isAlpha()用于根据凯撒密码的规律判断是否是英文,数字原样输出,英文按照凯撒规律后移    #chr((ord(c)-x+i)%26+x)对于出现“ZZZZZ”或者“zzzzz”的情况做处理
总结Python enumerate() 函数:用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。Python3 islower()方法:用于检测字符串是否由小写字母组成。Python isalpha()方法:用于检测字符串是否只由字母组成。看到题目是一道有关凯撒密码的算法题,身为ctfer的我一阵狂喜,但在写完代码的调试过程中又被泼了一阵冷水,俄罗斯人真的很厉害,学到了。 总结

以上是内存溢出为你收集整理的每日codingame小游戏练习[2021.4.01](python入门学习)全部内容,希望文章能够帮你解决每日codingame小游戏练习[2021.4.01](python入门学习)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1188100.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存