Caesar cipher · Mã Caesar
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.
Mã Caesar
Nhà sử học La Mã Suetonius viết rằng Julius Caesar đã ẩn các tin nhắn của mình bằng cách dịch chuyển mỗi chữ cái ba vị trí dọc theo bảng chữ cái.
Hàm alphabet.index(ch) trả về vị trí của một chữ cái, từ 0 cho a đến 25 cho z. Cộng giá trị dịch chuyển và sử dụng % 26, để cho vị trí 26 quay trở lại 0. Để giải mã một tin nhắn, hãy dịch chuyển lại một lần nữa bằng 26 trừ đi giá trị dịch chuyển ban đầu.
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". · Viết caesar(text, shift) dịch chuyển mỗi chữ cái thường shift vị trí dọc theo bảng chữ cái, quay vòng từ z về a. Giữ nguyên mọi ký tự khác. caesar("hello, world", 3) là "khoor, zruog".
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.