mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 06:33:44 +00:00
update rot13.py (#1790)
* update rot13.py * Update rot13.py * Type hints, doctests, URL to Wikipedia Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
a9f73e318c
commit
182e3042f8
|
@ -1,9 +1,19 @@
|
||||||
def dencrypt(s, n):
|
def dencrypt(s: str, n: int=13):
|
||||||
|
"""
|
||||||
|
https://en.wikipedia.org/wiki/ROT13
|
||||||
|
|
||||||
|
>>> msg = "My secret bank account number is 173-52946 so don't tell anyone!!"
|
||||||
|
>>> s = dencrypt(msg)
|
||||||
|
>>> s
|
||||||
|
"Zl frperg onax nppbhag ahzore vf 173-52946 fb qba'g gryy nalbar!!"
|
||||||
|
>>> dencrypt(s) == msg
|
||||||
|
True
|
||||||
|
"""
|
||||||
out = ""
|
out = ""
|
||||||
for c in s:
|
for c in s:
|
||||||
if c >= "A" and c <= "Z":
|
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 c >= "a" and c <= "z":
|
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:
|
else:
|
||||||
out += c
|
out += c
|
||||||
|
@ -11,14 +21,16 @@ def dencrypt(s, n):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
s0 = "HELLO"
|
s0 = input("Enter message: ")
|
||||||
|
|
||||||
s1 = dencrypt(s0, 13)
|
s1 = dencrypt(s0, 13)
|
||||||
print(s1) # URYYB
|
print("Encryption:", s1)
|
||||||
|
|
||||||
s2 = dencrypt(s1, 13)
|
s2 = dencrypt(s1, 13)
|
||||||
print(s2) # HELLO
|
print("Decryption: ", s2)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user