From 4879a46be1c056cceb46be12155796a75f070c68 Mon Sep 17 00:00:00 2001 From: Srivaishnavi Yaddanapudi <127314796+Y-Srivaishnavi@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:52:40 +0530 Subject: [PATCH 1/5] Fix number of rotations `N` as 13 (Resolves TheAlgorithms#12306) --- ciphers/rot13.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ciphers/rot13.py b/ciphers/rot13.py index b367c3215127..5deb87c5df2a 100644 --- a/ciphers/rot13.py +++ b/ciphers/rot13.py @@ -1,4 +1,4 @@ -def dencrypt(s: str, n: int = 13) -> str: +def dencrypt(s: str) -> str: """ https://en.wikipedia.org/wiki/ROT13 @@ -9,12 +9,13 @@ def dencrypt(s: str, n: int = 13) -> str: >>> dencrypt(s) == msg True """ + N = 13 out = "" for c in s: if "A" <= c <= "Z": - out += chr(ord("A") + (ord(c) - ord("A") + n) % 26) + out += chr(ord("A") + (ord(c) - ord("A") + N) % 26) elif "a" <= c <= "z": - out += chr(ord("a") + (ord(c) - ord("a") + n) % 26) + out += chr(ord("a") + (ord(c) - ord("a") + N) % 26) else: out += c return out From 5efef35d8d481b40f450f65418e59741a479d22b Mon Sep 17 00:00:00 2001 From: Srivaishnavi Yaddanapudi <127314796+Y-Srivaishnavi@users.noreply.github.com> Date: Tue, 29 Oct 2024 17:04:58 +0530 Subject: [PATCH 2/5] Add input validation and change function name to `apply_rot13` --- ciphers/rot13.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/ciphers/rot13.py b/ciphers/rot13.py index 5deb87c5df2a..d7c2bace3ba6 100644 --- a/ciphers/rot13.py +++ b/ciphers/rot13.py @@ -1,14 +1,19 @@ -def dencrypt(s: str) -> str: +def apply_rot13(s: str) -> str: """ - https://en.wikipedia.org/wiki/ROT13 + Performs a special case of the Caesar cipher. + Rotates the plaintext by 13 letters. + Also see: https://en.wikipedia.org/wiki/ROT13 + Example usage: >>> msg = "My secret bank account number is 173-52946 so don't tell anyone!!" - >>> s = dencrypt(msg) + >>> s = apply_rot13(msg) >>> s "Zl frperg onax nppbhag ahzore vf 173-52946 fb qba'g gryy nalbar!!" - >>> dencrypt(s) == msg + >>> apply_rot13(s) == msg True """ + if not isinstance(s, str): + return "The input must be a string. Please try again." N = 13 out = "" for c in s: @@ -24,10 +29,10 @@ def dencrypt(s: str) -> str: def main() -> None: s0 = input("Enter message: ") - s1 = dencrypt(s0, 13) + s1 = apply_rot13(s0) print("Encryption:", s1) - s2 = dencrypt(s1, 13) + s2 = apply_rot13(s1) print("Decryption: ", s2) From 2bef9b9cb35e7f55aa3544f4fd7f2e467fac3a1c Mon Sep 17 00:00:00 2001 From: Srivaishnavi Yaddanapudi <127314796+Y-Srivaishnavi@users.noreply.github.com> Date: Tue, 29 Oct 2024 17:06:56 +0530 Subject: [PATCH 3/5] Update documentation --- ciphers/rot13.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ciphers/rot13.py b/ciphers/rot13.py index d7c2bace3ba6..bd6b2ac89fd2 100644 --- a/ciphers/rot13.py +++ b/ciphers/rot13.py @@ -1,7 +1,7 @@ def apply_rot13(s: str) -> str: """ - Performs a special case of the Caesar cipher. - Rotates the plaintext by 13 letters. + Performs a special reversible case of the Caesar cipher. + Rotates a text by 13 letters. Also see: https://en.wikipedia.org/wiki/ROT13 Example usage: From 92d58b7109f529a0ba950acc5050a7d3d1fda4c2 Mon Sep 17 00:00:00 2001 From: Srivaishnavi Yaddanapudi <127314796+Y-Srivaishnavi@users.noreply.github.com> Date: Tue, 29 Oct 2024 17:21:43 +0530 Subject: [PATCH 4/5] Add `.split --- ciphers/rot13.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ciphers/rot13.py b/ciphers/rot13.py index bd6b2ac89fd2..60a380ef3ccd 100644 --- a/ciphers/rot13.py +++ b/ciphers/rot13.py @@ -27,13 +27,13 @@ def apply_rot13(s: str) -> str: def main() -> None: - s0 = input("Enter message: ") + s0 = input("Enter message: ").strip() s1 = apply_rot13(s0) print("Encryption:", s1) s2 = apply_rot13(s1) - print("Decryption: ", s2) + print("Decryption:", s2) if __name__ == "__main__": From 3a98cdab6e8298452368f689bb7d1ee5c0311d2e Mon Sep 17 00:00:00 2001 From: Srivaishnavi Yaddanapudi <127314796+Y-Srivaishnavi@users.noreply.github.com> Date: Tue, 29 Oct 2024 17:34:37 +0530 Subject: [PATCH 5/5] Change `N` into lowercase --- ciphers/rot13.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ciphers/rot13.py b/ciphers/rot13.py index 60a380ef3ccd..9a04c05df5af 100644 --- a/ciphers/rot13.py +++ b/ciphers/rot13.py @@ -14,13 +14,13 @@ def apply_rot13(s: str) -> str: """ if not isinstance(s, str): return "The input must be a string. Please try again." - N = 13 + n = 13 out = "" for c in s: if "A" <= c <= "Z": - out += chr(ord("A") + (ord(c) - ord("A") + N) % 26) + out += chr(ord("A") + (ord(c) - ord("A") + n) % 26) elif "a" <= c <= "z": - out += chr(ord("a") + (ord(c) - ord("a") + N) % 26) + out += chr(ord("a") + (ord(c) - ord("a") + n) % 26) else: out += c return out