Caesar cipher · 凯撒密码
Caesar cipher
The Roman historian Suetonius wrote that Julius Caesar hid his messages by shifting each letter three places along the alphabet.
alphabet.index(ch) gives a letter's position, from 0 for a to 25 for z. Add the shift and use % 26, so that position 26 wraps back to 0. To decode a message, shift it again by 26 minus the original shift.
凯撒密码
罗马历史学家苏埃托尼乌斯记载,尤利乌斯·凯撒把每个字母在字母表中后移三位来隐藏信息。
alphabet.index(ch) 给出字母的位置:a 是 0,z 是 25。加上移动的位数,再用 % 26,让位置 26 回到 0。要解密,就用 26 减去原来的位数,再移动一次。
Write caesar(text, shift) that moves every lowercase letter shift places along the alphabet, wrapping from z back to a. Leave every other character as it is. caesar("hello, world", 3) is "khoor, zruog". · 编写 caesar(text, shift),把每个小写字母在字母表中向后移动 shift 位,超过 z 就回到 a。其他字符保持不变。caesar("hello, world", 3) 是 "khoor, zruog"。
Click Run to see the output here. · 点击“运行”查看此处输出。