-1

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_.'?

Frank
  • 339
  • 8
  • 18
  • please show your attempts, SO is not a code-writing service – Pynchia Nov 01 '15 at 22:00
  • *"I'm quite stuck on this"* considering that you haven't shown anything, this looks like a "write this for me!" task. You can offer some money on codersclan. – Artjom B. Nov 01 '15 at 22:01
  • I'm sorry, I'm trying to teach myself python. I'm mainly looking for some type of direction for this problem, not the code itself. I'll test my attempts then update this. Sorry. – Frank Nov 01 '15 at 22:04
  • please describe input and output transformation. Read [this](http://stackoverflow.com/help/mcve) – Pynchia Nov 01 '15 at 22:07
  • 1
    Your question is misleading. It seems from the examples that you want to reverse the string and apply a Caesar cipher, but you're talking about ROT13 for some reason, which is a special case of a Caesar cipher. Since your alphabet seems to be 28 characters long, you can only do ROT14. Also, since `_` and `.` are nowhere near the uppercase letters, you will have to make special handling of those. – Artjom B. Nov 01 '15 at 22:45
  • How would I limit the alphabetical order to just 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_.'? – Frank Nov 02 '15 at 01:23

1 Answers1

1

Your code can be simply fixed:

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

You can fix it to use another order for characters by replacing:

    x = ord(c)
    res += chr(x + rot)

With say:

    x = alphabet.index(c)
    res += alphabet[(x + rot) % len(alphabet)]

Consider:

alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_.'

def rrot(rot, s):
    'returns reverse rotation by rot of s'
    res = ''
    for c in s:
        x = alphabet.index(c)
        res += alphabet[(x + rot) % len(alphabet)]
        copy = res[::-1]
    return copy

Using those bad examples:

>>> rrot(3, 'YO_THERE.')
'1HUHKWbR\\'
>>> rrot(1, 'SNQZDRQDUDQ')
'REVERSE[ROT'

Here the results of those examples:

>>> rrot(3, 'YO_THERE.')
'CHUHKWBR.'
>>> rrot(1, 'SNQZDRQDUDQ')
'REVERSE_ROT'

Another way would be to build a translation table like:

trans = dict(zip(alphabet, alphabet[rot:] + alphabet[:rot]))

once outside the loop and then use:

    res += trans[c]

Like this:

alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_.'

def rrot(rot, s):
    'returns reverse rotation by rot of s'
    trans = dict(zip(alphabet, alphabet[rot:] + alphabet[:rot]))
    res = ''
    for c in s:
        res += trans[c]
        copy = res[::-1]
    return copy

Which has the same results:

>>> rrot(3, 'YO_THERE.')
'CHUHKWBR.'
>>> rrot(1, 'SNQZDRQDUDQ')
'REVERSE_ROT'
Dan D.
  • 73,243
  • 15
  • 104
  • 123