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.
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.
To run the tests, run the command busted
from within the exercise directory.
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/
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'))
end)
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'))
end)
it('should treat "ch" like a single consonant', function()
assert.equal('airchay', translate('chair'))
end)
it('should treat "qu" like a single consonant', function()
assert.equal('eenquay', translate('queen'))
end)
it('should treat a consonant plus "qu" like a single consonant', function()
assert.equal('aresquay', translate('square'))
end)
it('should treat "th" like a single consonant', function()
assert.equal('erapythay', translate('therapy'))
end)
it('should treat "thr" like a single consonant', function()
assert.equal('ushthray', translate('thrush'))
end)
it('should treat "sch" like a single consonant', function()
assert.equal('oolschay', translate('school'))
end)
it('should treat "yt" like a single vowel', function()
assert.equal('yttriaay', translate('yttria'))
end)
it('should treat "xr" like a single vowel', function()
assert.equal('xrayay', translate('xray'))
end)
it('should translate entire phrases', function()
assert.equal('ickquay astfay unray', translate('quick fast run'))
end)
it('should ignore capitalization when translating', function()
assert.equal('ickQuay aStFay Unray', translate('Quick FaSt rUn'))
end)
it('should retain punctuation in translated phrases', function()
assert.equal('ellway, hatway avehay eway erehay?', translate('well, what have we here?'))
end)
it('should retain all whitespace', function()
assert.equal('abstay\tanday pacessay', translate('tabs\tand spaces'))
end)
end)
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
end
for _, v in ipairs(double_vowels) do
local i, _ = word:find('^' .. v)
if i == 1 then return w .. 'ay' end
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'
end
end
return function(sentance)
return sentance:gsub('%a+', translate)
end
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.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments
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.