2014-03-14

posted Mar 14, 2014, 5:30 AM by Samuel Konstantinovich   [ updated Mar 14, 2014, 6:29 AM ]

Homework (deadline 3/14/2014 1159pm): Submit your numToWords(n) which should work for -999,999,999 to 999,999,999. 

Do not use: hyphens or the word and. 

Just make it simple:

numToWords(123,001,032) -> "one hundred twenty three million one thousand thirty two"

There is a homework server slot. Submit a py file (which gets saved as a txt file). It does not matter what you name the file except that it must end in .py

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'



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:

Some of you will try this:

int(character) >= int('a')    #to compare their ascii values.


But there are easier ways!!!


character >='a'   #true when the char has a higher ord() value than ord('a')

string[0]>='a' #is true when the 1st character has a higher ord() value than ord(a)



1. make a function

rot13char(c)  which returns the rot13 value of a single character if it is a valid upper or lower case letter. It returns the original character otherwise.

rot13char('a')  'n'

rot13char('A')  'N'

rot13char('-')  '-'


2. then use that function to write:

rot13(s) which returns a string that is the rot13 equivalent of the string

rot13('CLERK')  'PYREX'

rot13('she-HEY!')  'fur-URL!'


You will need to loop through the original string, and build a new string starting with the empty string "", then adding on the converted characters one at a time!


3. Make new functions that rotate by a differnt number:  rotXchar(c,x) and rotX(s,x)

rotXchar('a',1) → 'b'

rotXchar('a',3) → 'd'

rotXchar('z',13)  'm'    #this is like rot13

rotX('abcxyz',1) → 'bcdyza'

rotX('what',13) → 'jung'

Comments