Last active
October 17, 2015 20:15
-
-
Save JPLeBreton/60e41a42bb640edae260 to your computer and use it in GitHub Desktop.
ROT-N variable-offset substitution encipher for Python3 (to decipher, enter negative offset)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from string import ascii_lowercase | |
def rot_n(in_string, n=13): | |
"returns copy of in_string shifted alphabetically by given offset N" | |
n %= len(ascii_lowercase) | |
char_shifts = ascii_lowercase[n:] + ascii_lowercase[:n] | |
out_string = '' | |
for char in in_string: | |
if not char.isalpha(): | |
out_string += char | |
continue | |
new_char = char_shifts[ascii_lowercase.find(char.lower())] | |
out_string += new_char.upper() if char.isupper() else new_char | |
return out_string | |
if __name__ == '__main__': | |
# for Python2 compat use raw_input instead | |
in_string = input('String to shift: ') | |
offset = int(input('Offset to shift string by (default 13): ') or 13) | |
print(rot_n(in_string, offset)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment