Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions ciphers/rot13.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
def dencrypt(s: str, n: int = 13) -> str:
def dencrypt(s: str) -> str:
"""
Performs ROT13 encryption/decryption on the input string `s`.

https://en.wikipedia.org/wiki/ROT13

>>> msg = "My secret bank account number is 173-52946 so don't tell anyone!!"
Expand All @@ -9,29 +11,30 @@ def dencrypt(s: str, n: int = 13) -> str:
>>> dencrypt(s) == msg
True
"""
out = ""
# Validate input
assert isinstance(s, str), "Input must be a string"

# Using list to accumulate characters for efficiency
out = []
for c in s:
if "A" <= c <= "Z":
out += chr(ord("A") + (ord(c) - ord("A") + n) % 26)
out.append(chr(ord("A") + (ord(c) - ord("A") + 13) % 26))
elif "a" <= c <= "z":
out += chr(ord("a") + (ord(c) - ord("a") + n) % 26)
out.append(chr(ord("a") + (ord(c) - ord("a") + 13) % 26))
else:
out += c
return out

out.append(c)
return "".join(out)

def main() -> None:
s0 = input("Enter message: ")

s1 = dencrypt(s0, 13)
s1 = dencrypt(s0)
print("Encryption:", s1)

s2 = dencrypt(s1, 13)
print("Decryption: ", s2)

s2 = dencrypt(s1)
print("Decryption:", s2)

if __name__ == "__main__":
import doctest

doctest.testmod()
main()