Pig Latin

June 2, 2009

Pig Latin is an English-language word game, usually played by children, in which words are mutated systematically. A word that begins with a vowel (a, e, i, o, or u) has the syllable “way” added to the end; for instance, art becomes art-way and eagle becomes eagle-way. A word that begins with one or more consonants has the initial consonants stripped, moved to the end of the word, then the syllable “ay” is added; for instance, start becomes art-stay and door becomes oor-day. A hyphen is added as shown above as an aid to prevent ambiguity; otherwise, a word like aspray could be the translation of spray (ay-spray) or prays (ays-pray). Even so, some words remain ambiguous; art-way could be translated as either art or wart.

Your task is to write functions that translate English to Pig Latin and Pig Latin to English. When you are finished, you may read or run a suggested solution, or post your solution or discuss the exercise in the comments below.


Pages: 1 2

4 Responses to “Pig Latin”

  1. […] Praxis – Pig Latin By Remco Niemeijer Today‚Äôs Programming Praxis problem is about Pig Latin. Our target is 11 lines (the size of the provided […]

  2. Remco Niemeijer said

    My Haskell solution (see http://bonsaicode.wordpress.com/2009/06/02/programming-praxis-pig-latin/ for a version with comments):

    toPigLatin :: String -> String
    toPigLatin w = let (h, t) = break (flip elem "aeiouAEIOU") w
                   in t ++ "-" ++ (if null h then "w" else h) ++ "ay"
    fromPigLatin :: String -> String
    fromPigLatin w = let ('y':'a':h, '-':t) = break (== '-') $ reverse w
                     in reverse $ t ++ if h == "w" then "" else h
    pigLatin :: String -> String
    pigLatin w = if elem '-' w then fromPigLatin w else toPigLatin w
    main :: IO ()
    main = do
        print . map pigLatin $ words "art eagle start door spray prays wart"
        print . map pigLatin $ words
            "art-way eagle-way art-stay oor-day ay-spray ays-pray art-way"
  3. Jieren Chen said

    vowellist = [‘a’,’e’,’i’,’o’,’u’,’A’,’E’,’I’,’O’,’U’]

    def e2pword(engword):
    if engword[0] in vowellist:
    return engword + “-way”
    return engword[1:] + “-” + engword[0] + “ay”

    def e2p(engstr):
    return ” “.join([e2pword(word) for word in engstr.split(‘ ‘)])

    def p2eword(pigword):
    pigsplit = pigword.split(‘-‘)
    if pigsplit[1][0]==’w’:
    return pigsplit[0]
    return pigsplit[1][0]+pigsplit[0]

    def p2e(pigstr):
    return ” “.join([p2eword(word) for word in pigstr.split(‘ ‘)])

  4. jcs said

    This problems cries out for regular expressions. Here’s a solution in Emacs lisp:

    (defun eng->pl (wd)
      (if (member (aref wd 0) '(?a ?e ?i ?o ?u))
          (concat wd "-way")
        (replace-regexp-in-string "^\\([^aeiou]+\\)\\(.*\\)$" "\\2-\\1ay" wd)))

    (defun pl->eng (wd)
      (string-match "^\\([^-]+\\)-\\(.*\\)ay$" wd)
      (if (string= (match-string 2 wd) "w")
          (match-string 1 wd)
        (concat (match-string 2 wd) (match-string 1 wd))))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: