Lecture 21 (LAB06)

posted Mar 4, 2013, 7:10 PM by Samuel Konstantinovich   [ updated Mar 6, 2013, 5:31 AM ]
LAB06 continued.

IF you completed the original version of the lab from yesterday: 
you DON'T have to redo it! Just do part 2+4. Then try to make rotX(words,x) where x is how many characters to rotate.

OVERVIEW:
As a rule, it is better to break down your problem into small chunks and make each part a function. Using the different parts together to solve your problem is much easier to read and debug. (Debug each part separately as you do it)

Rather than do everything in one function, you should make a function that just rotates one character. After you do that, you can apply that function to each character of the string. This separates the 2 parts of the problem, the rotation of letters, and the looping over the string. 

Reminder:
Regarding yesterday's notes: stringVariable[x] is ANY variable that contains a string. x[1] y[1] s[3] etc. 

name="Bobby!!!"
you can use a name[x] to loop through the string where x is any integer from 0 to len(name)-1.

Hint:

name="David"

if name[0]>="a" and name[0]<="z":

    print "The first letter is lower case"

else:

    print "The first letter is NOT lower case"


WARMUP: Write a function to print out the ASCII table (characters 33-90) in the following format:
33: !
34: "
35: #
36: $
.
.
65: A
66: B
67: C
68: D
69: E
.
.
90:  Z

1a. Write a function rotChar13a(c) that takes a single lower case character and rotates it by 13.
examples:
rotChar13a('a') returns 'n'
rotChar13a('m') returns  'z'

1b. Make rotChar13b(c) work on upper AND Lower case letters.
examples:
rotChar13b('C') returns  'P'
rotChar13b('P') returns  'C'

1c. Make rotChar13(c) , it should work on any character. If the character is upper or lower case, treat it like before, otherwise leave the character alone. 
examples:
rotChar13('C') returns  'P'
rotChar13('c') returns  'p'
rotChar13('!') returns  '!'
rotChar13(' ') returns  ' '  #space is a symbol.


STOP AND THINK: 
2. Now you should be able to modify your function to print out the ASCII table to include the ROT13 value of that character next to it in the following format:
     chr       rot13
33: !        !
34: "        "
.
.
.
65: A N
66: B  O
67: C P
68: D Q
.
.
.
90: Z        M


3. Make a function rot13(s) that takes a string as a parameter. You should rotate each of its characters by 13 positions (using the functions you already defined) 
Examples:
rot13('pyrex') returns  'clerk'
rot13('pyRex Clerk!') returns  'clErk Pyrex!'

4. Decode the following messages:
"Gjnf oevyyvt naq gur fyvgul gbirf, qvq tler naq tvzoyr va gur jnor."
"Qb, be qb abg. Gurer vf ab gel!"
"V'yy or Onpu. Lbh or Zbmneg."
Comments