The rear-view mirror is a cipher with a built-in retrospect.

When encoding a message, each letter is encoded as the sum of the letter and the preceding letter in the message. Each letter has a value that corresponds to the position of the letter in the alphabet: A=0, B=1, C=2, …, Z=25. The addition is performed modulo 26 (the number of letters in the alphabet). For example, suppose we have to encode the letter D and that letter is preceded in the message by the letter Z, then we get \[ \text{D} + \text{Z} = (3 + 25)\ \text{mod}\ 26 = 2 = \text{C} \] The following rules also apply when encoding messages:

Decoding an encoded message is done by rolling back the encoding process. To decode a letter from the encoded message, the preceding letter in the original message is subtracted. Decoding must therefore be done from left to right. For example, suppose we have to decode the letter C and the preceding letter in the message has already been decoded as Z, then we get \[ \text{C} - \text{Z} = (2 - 25)\ \text{mod}\ 26 = 3 = \text{D} \]

Assignment

Example

>>> encode_letter('d', 'Z')
'c'
>>> encode_letter('T', 'e')
'X'

>>> decode_letter('c', 'Z')
'd'
>>> decode_letter('X', 'e')
'T'

>>> encode('Saw things clearer once you were in my rear-view mirror.')
'Ssw papvty unpervv fbpg cmi qavv mv zk pver-mdma iuziff.'
>>> encode('The past is always tense. The future perfect.')
'Tal tpsl ba slhwyq lxrfw. Xal jznnlv ttvwjgv.'

>>> decode('Ssw papvty unpervv fbpg cmi qavv mv zk pver-mdma iuziff.')
'Saw things clearer once you were in my rear-view mirror.'
>>> decode('Tal tpsl ba slhwyq lxrfw. Xal jznnlv ttvwjgv.')
'The past is always tense. The future perfect.'

Epilogue

self-regard
Edward O'Brien patented this "body-attached rear-view mirror" in 1905.

Edward O'Brien patented this "body-attached rear-view mirror" in 1905

"… to facilitate the dressing of the hair and the inspection of the back of the head and head dress."

Essentially it's a harness that bears three mirrors and an illuminating bulb, replacing a bothersome hand mirror.

"By this means, both hands of the wearer are free to properly arrange the head dress, brush the hair and the like, without disturbing the adjustment of the mirror and illuminating means."

Just remember to take it off afterward …