Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A very simplistic scheme, which was used at one time to encode information, is t

ID: 3759380 • Letter: A

Question

A very simplistic scheme, which was used at one time to encode information, is to rotate the characters within an alphabet and rewrite them. ROT13 is the variant in which the characters A-Z are rotated 13 places, and it was a commonly used insecure scheme that attempted to "hide" data in many applications from the late 1990's and into the early 2000's.

It has been decided by Insecure Inc. to develop a product that "improves" upon this scheme by first reversing the entire string and then rotating it. As an example, if we apply this scheme to string ABCD with a reversal and rotation of 1, after the reversal we would have DCBA and then after rotating that by 1 position we have the result EDCB.

Your task is to implement this encoding scheme for strings that contain only capital letters, underscores, and periods. Rotations are to be performed using the alphabet order:

ABCDEFGHIJKLMNOPQRSTUVWXYZ_.

Note that underscore follows Z, and the period follows the underscore. Thus a forward rotation of 1 means 'A' is shifted to 'B', that is, 'A''B', 'B''C', ..., 'Z''_', '_''.', and '.''A'. Likewise a rotation of 3 means 'A''D', 'B''E', ..., '.''C'.

Implement the function rrot() that takes as input an integer rot (where 1 rot 27) and a string s and returns the "encrypted" copy of s that results after s is reversed and then shifted by rot.

Usage:
>>> rrot(1, 'ABCD')
'EDCB'
>>> rrot(3, 'YO_THERE.')
'CHUHKWBR.'
>>> rrot(1, '.DOT')
'UPEA'
>>> rrot(1, 'SNQZDRQDUDQ')
'REVERSE_ROT'

Explanation / Answer

The function rrot(rot, s) that takes as input an integer rot and string s and returns a copy of s that s is reversed and then shifted by rot.

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

The above code will provide the correct output for the given usage:

>>> rrot(1, 'ABCD')

'EDCB'

>>> rrot(3, 'YO_THERE.')

'CHUHKWBR.'

>>> rrot(1, '.DOT')

'UPEA'

>>> rrot(1, 'SNQZDRQDUDQ')

'REVERSE_ROT'

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote