Skip to content

update rot13.py #1790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 8, 2020
Merged
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
24 changes: 18 additions & 6 deletions ciphers/rot13.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
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 = ""
for c in s:
if c >= "A" and c <= "Z":
if "A" <= c <= "Z":
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)
else:
out += c
return out


def main():
s0 = "HELLO"
s0 = input("Enter message: ")

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

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


if __name__ == "__main__":
import doctest
doctest.testmod()
main()