Lecture 20

posted Mar 4, 2013, 6:15 AM by Samuel Konstantinovich   [ updated Mar 6, 2013, 5:30 AM ]

DO NOW:

Discuss with your neighbors:

How can you convert a character to the character after it:

f('a') → 'b'

f('S') → 'T'

Remember you can convert characters to integers and integers to characters using chr() and ord()

Ord gives you the numerical value of a character

ord('a') --> 97  

Chr gives you the character of a numerical value

chr(97) --> 'a'


Go Over Quiz:

Several Solutions:

def duplicate(START,N):

    result = ''

    x=0

    while x<N:

        result = result + str(START+x)+','

        x=x+1

    return result


def duplicate(START,N):

    result = ''

    x=START

    while x<START+N:

        result = result + str(x)+','

        x=x+1

    return result


def duplicate(START,N):

    result = ''

    begin = START

    end=N+START

    while begin<end:

        result = result + str(begin)+','

        begin=begin+1

    return result


#coolest way I saw:

def duplicate(START,N):

    result = ''

    while START<START+N:  #this stops when N reaches 0.

        result = result + str(START)+','

        START=START+1

        N=N-1

    return result




ROT13:

An “encryption” algorithm that changes a string by shifting its letters by 13 positions in the alphabet.

Some examples:

BIG → OVT

BOTS → OBGF

HEY → URL

NOPS → ABCF

she → fur

clerk → pyrex

Note that ROT13 is a poor encryption method, because it is trivial to decrypt. This made it useful for posting spoilers or text that some people didn't want to see, because you must actively change the words in order to read it.


LAB06: (please use the function names I give you)


Basic algorithm:

We can go through a string, and decide what to do with each character. You can compare characters, so you can compare letters different ways:

stringVariable[0]>='a' to see if the letter of the string is an 'a' or higher. Remember that upper and lower case are not comparable

int(stringVariable[0]) >= int(a) will compare their ascii values.

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"

Comments