Rot13 Function

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'

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

Related Homework Solutions

Python Programming Assignment
\$23.00
Python Programming
Polygon
Pentagon
Program
Computer Science
Code
A Python Program for Personal Income Tax Form
\$25.00
Python
Programming
Project
Tax Form
Tax Table
IRS 1040EZ Form
Wages
Salaries
Tips
Python Functions: printMolecularWeight, textFileAnalysis and netProfitOrLoss
\$33.00
Computer
Science
Python
Programming
Functions
Molecule
Weight
File
Print
Profit
Data Collection Script Using Python
\$63.00
Computer Science
Programming
Python
Windows
DOS Shell Command
Scripts
Data Collection
Algorithms
Statements
Variables
Methods
SSID
Signal Strength
Information
Tables
Databases
Discrete Event Simulation
\$50.00
Computer
Science
Discrete
Event
Simulation
Python
Disease
Agents
Infection
Python Programming Assignment
\$40.00
Python Programming
Computer Science
Function
Currency Converter
Program
Live Chats