Last active
August 16, 2022 00:43
-
-
Save Purrrpley/71e207916df1ecf31b5b4b883e4a41e4 to your computer and use it in GitHub Desktop.
Encrypt and Decrypt Caesar Ciphers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cipher(string: str, offset: int) -> str: | |
result = '' | |
for char in string: | |
if 'a' <= char <= 'z': | |
start, end = 'a', 'z' | |
elif 'A' <= char <= 'Z': | |
start, end = 'A', 'Z' | |
else: | |
start, end = char, char | |
result += chr( | |
(ord(char) - ord(start) + offset) | |
% (ord(end) - ord(start) + 1) | |
+ ord(start) | |
) | |
return result | |
def decrypt_all(string: str) -> list[str]: | |
return [cipher(string, i) for i in range(26)] | |
# >>> print(cipher('and', 3)) | |
# dqg | |
# >>> print(*decrypt_all('and'), sep='\n') | |
# and | |
# boe | |
# cpf | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment