SFind("Hi there","i") # returns 1 SFind('Hi there','I') # returns -1 SFind("Hi there, whatever",'e') # returns 5 SFind('','r') # returns -11b. 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 -12a. 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.
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'