Pig Latin Translator

From WLCS
Revision as of 15:00, 7 January 2009 by Admin (talk | contribs)

Objective:

  • You will create a program that translates English to Pig Latin

Translation Rules:

  1. If a word has no letters, don't translate it.
  2. 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.
  3. 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.
  4. 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.

Directions:

  1. Your first task is to produce a function named getPrefix(s) that takes one argument, 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.
  2. Your next task is to produce a function named getStem(s) that takes one argument, a string, and returns the stem portion of the word including the first vowel.
  3. Create a function named translateWord(s) that takes one argument, a string, and 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)

Examples:

--> stop
opstay
--> littering
itteringlay
--> buddy
uddybay

Testing:

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'
    """

if __name__ == '__main__':
    import doctest
    doctest.testmod()