Cipher [25
pts]
Background:
http://en.wikipedia.org/wiki/Substitution_cipher#Simple_substitution
A substitution cipher is a basic method of encryption.
Given a jumbled version of the alphabet, each letter in a message is replaced
with the letter that is in its position in the jumbled alphabet.
For example, using SQCDGYFEZBLANKJIMPURWTHVOX for a jumble means that
we would replace "A" with "S", "B" with "Q", "C" with "C", and so on, turning
"Hello, World!" into "Egaaj, Hjpad!" (Note that substitutions are case
sensitive!)
We first generate a jumbled alphabet from a string. The function alphabetHelper takes a string and returns a string of upper-case letters in the order they first appeared, and makeCipherAlphabet uses this to make an entire alphabet (by filling in the remaining letters). The function makeSubstitution takes one of these alphabets and applies the cipher, and so substitutionCipher can combine these together to take any two strings and encrypt the first using the second.
With this in mind, write a function substitutionCipher(key, message) that
takes a string, key, and a string, message, to encode. The function should
use the key to create a jumble and use this jumbled cipher alphabet for
substitution, then return the encoded message.
To make this problem more approachable, we are providing more specifics on each helper function. And to make the problem more gradeable, we are requiring that you follow this design! So you should solve the problem by writing the following functions in the order given:
-
alphabetHelper(text) [5 pts]
Write the function alphabetHelper(text) which takes a string, text, and returns the same string containing only unique uppercase letters. For example, alphabetHelper("Bb,Ac")
returns "BAC".
-
makeCipherAlphabet(key) [5 pts]
Write the function makeCipherAlphabet(key) which takes a string, key, and returns a string containing an uppercase, jumbled version of the alphabet starting with the string from alphabetHelper and then adding the remaining letters of the alphabet in their normal order. For example, makeCipherAlphabet("SQCDGYFEZBLANKJIMPURWTHVOX") returns "SQCDGYFEZBLANKJIMPURWTHVOX" and makeCipherAlphabet("Zebras? Zsa!") returns "ZEBRASCDFGHIJKLMNOPQTUVWXY".
-
makeSubstitution(message, cipherAlphabet) [10 pts]
Write the function makeSubstitution(message, cipherAlphabet) which takes two strings: message and cipherAlphabet. The function should use the substitution cipher described above (using cipherAlphabet for the substitution) and return an encoded version of message. For example, makeSubstitution("Hello, World!", "SQCDGYFEZBLANKJIMPURWTHVOX") should return "Egaaj, Hjpad!" (Again note that substitutions are case sensitive, and non-characters are unchanged.).
-
substitutionCipher(message, key) [5 pts]
Finally, write the function substitutionCipher(message, key) which takes two strings: message and key. The function should use makeCipherAlphabet(key) to create a cipher alphabet which is used in makeSubstitution(message, cipherAlphabet) to encode the original message (of course using the substitution cipher described above). For example, substitutionCipher("Hello, World!", "Zebras? Zsa!") should return "Daiil,
Vloir!".
pigLatin
[30 pts]
Background: Pig Latin is an alteration of English. To make a word in Pig Latin, take an English word, move
the
leading consonants to the end, followed by " ay " (e.g. " school " -> " oolschay
").
If the word begins with a vowel, or if it contains no vowels,
then simply add " ay " to the end (e.g. " in " -> " inay ").
Write a function named pigLatin which takes a string of English words and translates it into Pig Latin. The
function should return the result as a string.
You should treat "qu" as a single consonant, and "y" should be
treated as a vowel.
Words are divided by spaces, and any non-alphabetic characters (punctuation, numbers, etc.) inside a word should remain where they are. If
outside of a word, they should remain outside (e.g. "hey, don‘t do that!" -> "eyhay, on‘tday
oday atthay!"). If a "word" (such as "9000") contains no letters, do not add "ay" to the end.
Finally, if the first letter of a word is capitalized, then the first letter of the pig latin word should be capitalized (e.g. "Professor Kosbie"
->
"Ofessorpray Osbiekay"). All letters except the first will be lowercase in the input, and should be lower case in your output as in the previous example.
carpe diem - carpe
diem - carpe diem - carpe diem - carpe diem
- carpe diem -
carpe diem - carpe diem - carpe diem