Pig Latin Translator
Revision as of 09:20, 2 January 2018 by Admin
- You will create a program that translates English to Pig Latin
- If a word has no letters, don't translate it.
- Separate each word into two parts. The first part is called the prefix and extends from the beginning of the word up to, but not including, the first vowel. (The letter y will be considered a vowel.) The rest of the word is called the stem.
- The Pig Latin text is formed by reversing the order of the prefix and stem and adding the letters ay to the end. For example, sandwich is composed of s + andwich + ay + . and would translate to andwichsay.
- If the word contains no consonants, let the prefix be empty and the word be the stem. The word ending should be yay instead of merely ay. For example, I would be Iyay.
- Your first task is to produce a function named getPrefix(s) that takes one parameter, a string, and returns the portion of the word up to, but not including the first vowel. For example, if you sent 'banana' to your function, it should return 'b'. Sending 'Sibley' should return 'S', 'stream' should return 'str', and 'break' should return 'br'. Print out your working function and a sample run.
- You will use a loop to find the first vowel.
- Initialize a loop counter to start at 0
- Traverse the string s
- Inside the loop, check if the currently indexed letter matches the vowels (this will have a lot of == and or)
- If you find a vowel, then return string slice from 0 up to the index
- Outside of the loop return an empty string (no vowels were found if this line of code runs)
- Your next task is to produce a function named getStem(s) that takes one parameter, a string, and returns the stem portion of the word including the first vowel.
- You should use a loop to find the first vowel. After that, return the string slice from the index of the vowel to the end of the string (len(str))
- Hint: getStem(s) is very similar to getPrefix(s)
- Create a function named translateWord(s) that takes one parameter, a string s
- Use function calls to getPrefix(s) and getStem(s) to return the Pig Latin translated word.
- You should also be sure that you follow the rules of Pig Latin listed above (e.g. If the word contains no consonants, just add yay)
--> stop opstay --> littering itteringlay --> buddy uddybay
def translateWord(s): """ >>> translateWord("hello") 'ellohay' >>> translateWord("bob") 'obbay' >>> translateWord("a") 'ayay' >>> translateWord("washington-lee") 'ashington-leeway' >>> translateWord("pants") 'antspay' >>> translateWord("littering") 'itteringlay' >>> translateWord("buddy") 'uddybay' """ #YOUR CODE WOULD GO HERE if __name__ == '__main__': import doctest doctest.testmod()
Additional String Features:
- Python strings actually have methods/functions built into them. You can read them at Python String Methods. Pay close attention to the string.split() and string.lower() functions/methods
- Try out the following code:
s = "Here is a string with several words in it" wordList = s.split() print(wordList) #What do you see in wordList? #Try out the same code, but using s.lower() this time...what is the difference?
Complete the Pig Latin Translator:
- Create a function called translateSentence(s) that takes one string parameter (it'll be a whole sentence)
- First, split the string into a list of words
- Traverse the list of words and call translateWord() on each word
- If you do it properly, you'll be able to translate an entire sentence into Pig Latin!