String Exercises

Some of these exercises require repetition, so use WHILE or FOR and not recursion. You may use String methods, unless the problem specifically prohibits it.

1a. There is a string method called upper() which returns a new string converting all lower-case characters in the original string to upper-case, and leaving all other characters unchanged. 
And so, if s="Hi there, 2 you", then s.upper() is "HI THERE, 2 YOU".  We'd like to duplicate the effect of the upper() method with our own function: ToUpper(s), which will return the upper-case version of s (leaving all non lower-case letters in the string unchanged)...

... and here's a way to do that.  For each letter in the original string, try to find the position of that letter (using SFind()) in the lower-case alphabet string: "abcdefghijklmnopqrstuvwxyz".  If it's there, then that character was a lower-case letter, and now we know its position, and so we can use that position to find the corresponding upper-case letter in the alphabet string "ABCDEFGHIJKLMNOPQRSTUVWXYZ".  However, if the letter was not found in the lower-case alphabet, then we do not want to change it.  And so, we can build up the new string letter-by-letter this way.  OK, create ToUpper().  Example:
	ToUpper("Hi there, 2 you")  # returns 'HI THERE, 2 YOU'

1b. Well, if we can convert one set of characters (lower-case letters) into another (upper-case), then we can convert any set of characters into any other.  Why might we want to do this?  How about encryption?  Let's try to encrypt text the way Julius Caesar probably did (were he to have known English and Python).  While in problem 1a we converted from the sequence before and to the sequence after (see below)...

	before='abcdefghijklmnopqrstuvwxyz'
	after ='ABCDEFGHIJKLMNOPQRSTUVWXYZ'

...we can just replace those with other sequences.  For instance, Julius' coding converted each letter to a letter 3 places before it: D became A, E became B, Z became W, and A became X, etc.  So, here are the new sequences:

	Julius_before='defghijklmnopqrstuvwxyzabcDEFGHIJKLMNOPQRSTUVWXYZABC'
	Julius_after ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Create the the function Encrypt(some_text) that takes a string and returns a new string encrypted by replacing each letter with a letter 3 spaces back (using Julius_before and Julius_after). And while you're at it, create Decrypt(encrypted_text) that turns that encrypted text back into the original readable "plain-text".

2a. People's names can be spelled with various capitalizations, (for instance: 'John Smith' and 'john smith' and 'JOHN SMITH'), and yet they're all the same name.  Write a function called IsSameName(name1,name2) that will return True if the two names are the same, and False otherwise.

    IsSameName('John smith', 'JOHN Smith') # returns True
    IsSameName('John Smith', "John's Myth")  # returns False

2b.  Create the function CapWord(word) that capitalizes a single word in exactly the same way that the built-in string method s.capitalize() does.  Of course, you may NOT use that built-in string method.

2c. Create the function CapName(name) that capitalizes a name consisting of a first name and last name separated by a space.  Example:
   CapName('joHN SMith')  # returns 'John Smith'

3a. Officially, names are often listed with lastname first, as in "Brooks, Peter".  Let's write a function to correct that: FirstLast(name) will be given a string with a lastname followed by a comma and a space, followed by the firstname. It will return a string with the firstname, followed by a space, followed by the lastname.  Use string slicing, and either SFind() from problem 1 or an equivalent string method.  Example:
	FirstLast('Brooks, Peter')  # returns 'Peter Brooks'

3b. Assume that you have a long string consisting of a sequence of people's "lastname, firstname" pairs terminated by ";" (semi-colons).  Convert them to " firstname lastname" pairs terminated by semi-colons. There can be any number of these name-pairs in the string.  Call it FirstLastSequence(names).  Example:

	FirstLastSequence('Brooks, Peter;Holmes, David;Pascu, Ms.;') # returns 'Peter Brooks;David Holmes;Ms. Pascu;'

4. It is usually important to know what kind of data is inside a file.  The creator of a file usually helps by naming the file with a standard file suffix (like .mp3 or .jpg), to indicate that the file contains music or a picture.  Create the function FileClassifier(filename) that will be given the name of a file, and will return its type.  Note that the filename suffix may be in UPPER-CASE (.JPEG) or lower-case (.jpeg) and should still be classified correctly.   You may use any string methods you find appropriate (or none). Here's how it should classify:

suffix type
jpg picture
jpeg picture
mp3 music
nlogo Netlogo
py Python
	FileClassifier('StarSpangledBanner.Mp3')  # returns 'music'
	FileClassifier('Fred.mp3.JPEG.nlogo')     # returns 'Netlogo'

 
Triple Challenge Problem:  Let's work with Caesar ciphers like we did in problem 1b.  This is just a shift of lower-case characters by a certain number of positions (say, n positions), and the same shift of n positions for the upper-case characters as well.  So if we knew what the value of n was, we could decrypt a message that was encrypted using this technique.  Well, what if we have an encrypted message but we don't know what n is?  How can we decrypt such a message?  One way to do that is to make a guess for n and try decrypting the message with that n and see if it looks like something that we can read -- does it have English words?  I am going to give you a quotation from a famous scientist, that's been encryted using the Caesar encryption technique, but I'm not going to give you the value of n.   However, to make the problem harder (this is why I pull down the big bucks), I'm not going to allow you to use your knowledge of English to figure this out.  I will, however, give you another clue: the name of one of the NYC professional sports teams is one of the words in the actual quotation. 

So, here is your task ... the encrypted message is:

	Zw Z yrmv jvve wlikyvi zk zj sp jkreuzex fe kyv jyfcuvij fw Xzrekj.
You have to write the function TCP(s) which will be given the string above, and will return the original quotation (and copy the quotation into the Comments-to-Teacher).  The algorithm that figures it out will need to include the fact that one of the words in the quotation is the name of a professional NY sports team.  You CANNOTjust use your knowledge of English and read the various attempts at decryption and visually spot the one that makes sense.  The answer must be found solely by your Python function.  BTW, this is what cryptanalysts do.