🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of JaeHyoLee

JaeHyoLee's solution

to Pig Latin in the Lua Track

Published at Jul 13 2018 · 1 comment
Test suite

Implement a program that translates from English to Pig Latin.

Pig Latin is a made-up children's language that's intended to be confusing. It obeys a few simple rules (below), but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand.

  • Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
  • Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word.

There are a few more rules for edge cases, and there are regional variants too.

See http://en.wikipedia.org/wiki/Pig_latin for more details.

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.


The Pig Latin exercise at Test First Teaching by Ultrasaurus https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


local translate = require('pig-latin')

describe('pig-latin', function()
  it('should append "ay" to words beginning with a vowel', function()
    assert.equal('appleay', translate('apple'))
    assert.equal('earay', translate('ear'))
    assert.equal('iglooay', translate('igloo'))
    assert.equal('objectay', translate('object'))
    assert.equal('underay', translate('under'))

  it('should move the first letter plus "ay" to the end of words starting with a consonant', function()
    assert.equal('igpay', translate('pig'))
    assert.equal('oalakay', translate('koala'))
    assert.equal('ellowyay', translate('yellow'))
    assert.equal('enonxay', translate('xenon'))
    assert.equal('atqay', translate('qat'))

  it('should treat "ch" like a single consonant', function()
    assert.equal('airchay', translate('chair'))

  it('should treat "qu" like a single consonant', function()
    assert.equal('eenquay', translate('queen'))

  it('should treat a consonant plus "qu" like a single consonant', function()
    assert.equal('aresquay', translate('square'))

  it('should treat "th" like a single consonant', function()
    assert.equal('erapythay', translate('therapy'))

  it('should treat "thr" like a single consonant', function()
    assert.equal('ushthray', translate('thrush'))

  it('should treat "sch" like a single consonant', function()
    assert.equal('oolschay', translate('school'))

  it('should treat "yt" like a single vowel', function()
    assert.equal('yttriaay', translate('yttria'))

  it('should treat "xr" like a single vowel', function()
    assert.equal('xrayay', translate('xray'))

  it('should translate entire phrases', function()
    assert.equal('ickquay astfay unray', translate('quick fast run'))

  it('should ignore capitalization when translating', function()
    assert.equal('ickQuay aStFay Unray', translate('Quick FaSt rUn'))

  it('should retain punctuation in translated phrases', function()
    assert.equal('ellway, hatway avehay eway erehay?', translate('well, what have we here?'))

  it('should retain all whitespace', function()
    assert.equal('abstay\tanday   pacessay', translate('tabs\tand   spaces'))
local vowels = "[aeiou]"
local consonant = "[^aeiou]"
local double_consonant = {'sch', 'ch', 'qu', 'thr', 'th'}
local double_vowels = {'yt', 'xr', 'qu', 'thr', 'th'}

local function translate(w)
  local word = w:lower()
  for _, v in ipairs(double_consonant) do
    local i, _ = word:find('^' .. v)
    if i == 1 then return w:sub(#v+1) .. w:sub(1,#v) .. 'ay' end

  for _, v in ipairs(double_vowels) do
    local i, _ = word:find('^' .. v)
    if i == 1 then return w .. 'ay' end

  local i, _ = word:find('^' .. vowels)
  if i == 1 then return w .. 'ay' end

  local i, j = word:find('^' .. consonant)
  if i == 1 then
    if word:sub(2,3) == 'qu' then j = 3 end
    return w:sub(j+1) .. w:sub(i,j) .. 'ay'

return function(sentance)
  return sentance:gsub('%a+', translate)

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of ryanplusplus

On line 18, it's not necessary to include the _ because extra return values are automatically omitted if you don't provide a variable in which they can be stored.

Looks great otherwise.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?