Subject Computer Science Python Programming

Question

Rot13 is a simple encoding popular in the 1970's and 1980s for "encoding" text that folks didn't want innocent people (e.g. young children) to read unintentionally; that is one has to intentionally "decode" the encoded text to make sense of it. The code is in fact very simple; one simply rotates each alphabetic letter character 13 positions in the alphabet; this is applied to every letter character in the string; non-letters are not rotated. For example,
rot13('a') => 'n', rot13('b') => 'o', rot13('z') => 'm'. Since each character in the string argument is rotated, we can encode messages; e.g.,

>>> s = rot13('one day we will be free.')
>>> s
'bar qnl jr jvyy or serr.'
Notice, that since we are rotating letters 13 positions, and because there are 26 characters, the same function, rot13(), can be used to decode encoded text, e.g.,
>>> rot13('bar qnl jr jvyy or serr.')
'one day we will be free.'
And,
>>> rot13('abcdefghijklmnopqrstuvwxyz')
'nopqrstuvwxyzabcdefghijklm'
>>> rot13('nopqrstuvwxyzabcdefghijklm')
'abcdefghijklmnopqrstuvwxyz'
>>> rot13('a')
'n'
>>> rot13('n')
'a'
OK, now for your assignment.

Implement the function rot13() so that it rotates all lowercase letters by 13. That is, it should work as defined above.
Implement a second version, rot13a() such that it deals with both uppercase and lowercase letters; non letters are left along. Eg,
code
>>> rot13a('One Day We Will Be Free.')
'Bar Qnl Jr Jvyy Or Serr.'
rot13a('Bar Qnl Jr Jvyy Or Serr.')
'One Day We Will Be Free.'
[[code]]

Extra Credit:

Write two functions:
encode(s,x), which rotates string s x characters in the alphabet; both uppercase and lowercase letters should be rotated.
decode(s,x) which undoes the encoding, that is, rotates the letters pf s in the opposite direction x positions.
Notice this is harder as encode differs from decode.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

def rot13(string):
    letters ='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
    newString =""
    for char in string:
       if (char in letters):
            newletter = letters[letters.find(char)+13]
       else:
            newletter = char
            
       newString += newletter...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor

Related Homework Solutions

Get help from a qualified tutor
Live Chats