Tuesday, November 30, 2010

Pig Latin Encoder

To encode a word in pig latin, we need to handle two cases as described on p. 277.

The following function is a partial (but not complete) solution.

For example, how does it encode swing? rhythm? Aardvaark? CIS?

/* return word w encoded as pig latin */
function encode(w){
if(w.search(/[aeiou]/) == -1 || w.search(/[aeiou]/) == 0)
return w + 'way'
else
return w.substring(1, w.length) + w.charAt(0) + 'ay'
}

MISC PIG LATIN NOTES

var w = 'oops!!'
var punct, bareWord, pLatin, ans

//peel off punctuation
punct = w.substring(w.search(/[!,.]/), w.length)
bareWord = w.substring(0, w.search(/[!,.]/))

pLatin = encode(bareword)

ans = pLatin + punct

Test Cases:

Eighthstreet, gym, myrrh, aeon, llama, kvetch.

gnome, bloom, brew, clue, snow, smart, wrench.

rhythm, crypt, rhyme, gypsy, strength, scrimp, shrimp, schlep, shrapnel, splice, sphygmomanometer, strap, scrap, spring,

Pig Latin koans: Xylophone, tsktsk, eau de Cologne.
Enhanced by Zemanta

No comments: