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());}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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!