Skip to content

Instantly share code, notes, and snippets.

@Purrrpley
Last active August 16, 2022 00:43
Show Gist options
  • Save Purrrpley/71e207916df1ecf31b5b4b883e4a41e4 to your computer and use it in GitHub Desktop.
Save Purrrpley/71e207916df1ecf31b5b4b883e4a41e4 to your computer and use it in GitHub Desktop.
Encrypt and Decrypt Caesar Ciphers
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