From 81d83abda9df4de3030233ef304ad4144fd19d21 Mon Sep 17 00:00:00 2001 From: Ayush Prem Date: Mon, 28 Oct 2024 17:53:48 +0530 Subject: [PATCH] Resolved issue #12306 Done for Hacktoberfest 2024 --- ciphers/rot13.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/ciphers/rot13.py b/ciphers/rot13.py index b367c3215..c092ac40a 100644 --- a/ciphers/rot13.py +++ b/ciphers/rot13.py @@ -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!!" @@ -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()