String Exercises


1a. Create the function SFind(some_string,letter), which will be given a string (of any length) and a single letter, and will return the position of the first occurrence of the letter inside the string.  If the letter is not found in the string, it returns -1.  You may not use any string methods (like .find() or .index()). Examples:
	SFind("Hi there","i")  # returns 1
	SFind('Hi there','I')  # returns -1
	SFind("Hi there, whatever",'e')  # returns 5
	SFind('','r')  # returns -1
1b. Create the function SRFind(some_string,letter), which will do the same thing as SFind() except it will return the position of the last occurrence of the letter.  Again, no string methods, please.  Examples:
	SRFind('Hi there','e')  # returns 7
	SRFind('Hi there','E')  # returns -1
2a. 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'

2b. 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 3a 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".

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. (challenge problem).  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'