Skip to content

Instantly share code, notes, and snippets.

@JPLeBreton
Last active October 17, 2015 20:15
Show Gist options
  • Save JPLeBreton/60e41a42bb640edae260 to your computer and use it in GitHub Desktop.
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)
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