Skip to content

Instantly share code, notes, and snippets.

@rohit-jamuar
Created July 31, 2014 05:48
Show Gist options
  • Save rohit-jamuar/7c265427352011656522 to your computer and use it in GitHub Desktop.
Save rohit-jamuar/7c265427352011656522 to your computer and use it in GitHub Desktop.
ROT13 encoder/decoder
#!/usr/bin/python
from string import lowercase, uppercase
ROTATED_LOWERCASE = lowercase[13 : ] + lowercase[ : 13]
ROTATED_UPPERCASE = uppercase[13 : ] + uppercase[ : 13]
REF = {}
for index1 in range(len(lowercase)):
REF[lowercase[index1]] = ROTATED_LOWERCASE[index1]
for index2 in range(len(uppercase)):
REF[uppercase[index2]] = ROTATED_UPPERCASE[index2]
def string_rotation(input_str):
'''
A function that takes a white-space separated string and returns
a rot13 version of that string with every other word reversed.
If the function is f, f(f(x)) returns x.
'''
if type(input_str) == str:
fin = []
input_list = input_str.split()
for index in range(len(input_list)):
temp = ''
if index % 2 == 0:
for char in input_list[index][::-1]:
if char in REF:
temp += REF[char]
else:
temp += char
else:
for char in input_list[index]:
if char in REF:
temp += REF[char]
else:
temp += char
fin.append(temp)
return ' '.join(fin)
else:
return 'The argument has to be a string!'
if __name__ == '__main__':
OUTPUT = string_rotation("Hello world! This is ROT13 :)")
print OUTPUT
print string_rotation(OUTPUT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment