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 :-/
require"minitest/autorun"classPigLatinTranslatorLATINATOR=->(word){word=~/\w/?"#{word[1..-1]}#{word[0]}ay":word}defself.translatesentencesentence.split(' ').map(&LATINATOR).join(' ')endendclassPigLatinTranslatorTest<MiniTest::Testdeftest_sentence_without_punctuationtranslated_sentence=PigLatinTranslator.translate("Pig latin is cool")assert_equal"igPay atinlay siay oolcay",translated_sentenceenddeftest_sentence_with_punctuationtranslated_sentence=PigLatinTranslator.translate("Hello world !")assert_equal"elloHay orldway !",translated_sentenceendend
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!
fnpigify_word(word:&str)->String{println!("{}",word);ifword.chars().all(|x|x.is_alphanumeric()){letmutchars=word.chars();letfirst_letter=chars.next().unwrap();letrest_of_letters:String=chars.collect();format!("{}{}ay",rest_of_letters,first_letter)}elseifword.chars().all(|x|!x.is_alphanumeric()){word.to_string()}else{letsymbol_index=word.find(|c:char|!c.is_alphanumeric()).unwrap();letalphanumeric_index=word.find(|c:char|c.is_alphanumeric()).unwrap();letindex=symbol_index.max(alphanumeric_index);[word[0..index].to_string(),word[index..].to_string()].iter().map(|x|pigify_word(x)).collect()}}pubfnpig_it(english_input:&str)->String{english_input.split(" ").map(pigify_word).collect::<Vec<_>>().join(" ")}#[cfg(test)]modtests{usecrate::*;#[test]fnit_works_without_punctuation(){assert_eq!(pig_it("Pig latin is cool"),"igPay atinlay siay oolcay".to_string());}#[test]fnit_works_with_spaced_out_punctuation(){assert_eq!(pig_it("Hello world !"),"elloHay orldway !".to_string());}#[test]fnit_works_with_word_ending_punctuation(){assert_eq!(pig_it("Hello world! How is it going today?"),"elloHay orldway! owHay siay tiay oinggay odaytay?".to_string());}#[test]fnit_works_with_word_mid_punctuation(){assert_eq!(pig_it("Hello world! How's it going today?"),"elloHay orldway! owHay'say tiay oinggay odaytay?".to_string());}}
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
Your pig latin rules are incorrect in the Examples. Words beginning with vowels do not move the first letter and traditionally end in 'way'. So pig_it('Pig latin is cool') should be # igPay atinlay isway oolcay
We're a place where coders share, stay up-to-date and grow their careers.
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
Here is the simple solution with Python:
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!
Elixir:
gist.github.com/devparkk/f27cf3cef...
Your pig latin rules are incorrect in the Examples. Words beginning with vowels do not move the first letter and traditionally end in 'way'. So pig_it('Pig latin is cool') should be # igPay atinlay isway oolcay