The goal of today's challenge is to translate English into Pig Latin. This challenge comes from user2505876 on CodeWars.
Write a function that moves the first letter of each word to the end of the word, then add
"ay"
. Leave punctuation untouched.Examples:
pig_it('Pig latin is cool') # igPay atinlay siay oolcay
pig_it('Hello world !') # elloHay orldway !
This one is somewhat tricky. I bet it would be even harder to turn Pig Latin back to English, I'd love to see someone try it~!
Oodgay ucklay, appyhay odingcay!
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (18)
CSS (kind of)
Separate the words in
<span>
s, and add the class "piglatin" to the container.The idea is based on floating the first letter to the right using the pseudo-class
:first-letter
, and then adding "ay" using the pseudo-element::after
... although it doesn't work well with punctuation :-/You can see a demo running on CodePen:
Screen readers still read the sentences right, even when visually they are in pig latin.
So happy to see another CSS solution! It's been awhile lol
Hell of a job!
Yes. The last ones have been impossible. But 4-5 out of 22 is not that bad :P
Oh, this is nice! ☺️
Here goes a JavaScript oneliner
And just for fun, an unPigIt oneliner too!
Nailed it 👌🔥🔥
Perl one-liner as well
This one handles leaving punctuation alone, so this would work as well:
No need to split and join:
Of course. Thanks!
python one liner ftw.
Ruby 2.6
Rust Solution!
I paid specific attention to the punctuation rule, and made sure to get that right!
I also wasn't satisfied with just the example punctuation because nobody spaces out their punctuation like that ! See doesn't that look weird ?
Also the spec doesn't mention at all with how to treat words with punctuation IN them! So I made my own spec here and decided to split the words on any punctuation, and treat each side as it own word. Proper pig latin? Maybe not! But it's doing what I intended!
Here is the simple solution with Python:
Elixir:
gist.github.com/devparkk/f27cf3cef...