I'm trying to implement a similar encoding scheme to that of the Caesar Cipher for strings that contain only capital letters, underscores, and periods. Rotations are to be performed using the alphabet order: ABCDEFGHIJKLMNOPQRSTUVWXYZ_.
Mycode so far:
def rrot(rot, s):
'returns reverse rotation by rot of s'
res = ''
for c in s:
x = ord(c)
res += chr(x + rot)
copy = res[::-1]
return copy
Some examples of outputs are:
>>> rrot(1, 'ABCD')
'EDCB'
>>> rrot(3, 'YO_THERE.')
'CHUHKWBR.'
>>> rrot(1, '.DOT')
'UPEA'
>>> rrot(1, 'SNQZDRQDUDQ')
'REVERSE_ROT'
But when ran, it operates around the whole alphabet including symbols {[/ etc. I get the correct shifting for the moajority of the letters but get unwanted symbols. My incorrect outputs:
>>> rrot(3, 'YO_THERE.')
'1HUHKWbR\\'
>>> rrot(1, 'SNQZDRQDUDQ')
'REVERSE[ROT'
But this is correct:
>>> rrot(1, 'ABCD')
'EDCB'
How to i get it to follow the alphabetical order of just the charactes 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_.'?