Skip to content

Commit 182e304

Browse files
update rot13.py (TheAlgorithms#1790)
* update rot13.py * Update rot13.py * Type hints, doctests, URL to Wikipedia Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent a9f73e3 commit 182e304

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

ciphers/rot13.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
def dencrypt(s, n):
1+
def dencrypt(s: str, n: int=13):
2+
"""
3+
https://en.wikipedia.org/wiki/ROT13
4+
5+
>>> msg = "My secret bank account number is 173-52946 so don't tell anyone!!"
6+
>>> s = dencrypt(msg)
7+
>>> s
8+
"Zl frperg onax nppbhag ahzore vf 173-52946 fb qba'g gryy nalbar!!"
9+
>>> dencrypt(s) == msg
10+
True
11+
"""
212
out = ""
313
for c in s:
4-
if c >= "A" and c <= "Z":
14+
if "A" <= c <= "Z":
515
out += chr(ord("A") + (ord(c) - ord("A") + n) % 26)
6-
elif c >= "a" and c <= "z":
16+
elif "a" <= c <= "z":
717
out += chr(ord("a") + (ord(c) - ord("a") + n) % 26)
818
else:
919
out += c
1020
return out
1121

1222

1323
def main():
14-
s0 = "HELLO"
24+
s0 = input("Enter message: ")
1525

1626
s1 = dencrypt(s0, 13)
17-
print(s1) # URYYB
27+
print("Encryption:", s1)
1828

1929
s2 = dencrypt(s1, 13)
20-
print(s2) # HELLO
30+
print("Decryption: ", s2)
2131

2232

2333
if __name__ == "__main__":
34+
import doctest
35+
doctest.testmod()
2436
main()

0 commit comments

Comments
 (0)