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 False2b. 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.
CapName('joHN SMith') # returns 'John Smith'
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. And, of course, no
shift for non-lettetrs. 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's major 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 jyflcuvij fw XzrekjYou 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.