Simple Pig Latin Codewars

0

We are going to solve pigIt challenge from the codewars. pigIt function accept a String which is str as an argument. So the string argument has to be translated into Pig Latin. For this we need to perform the following:

  1. Move the first letter of the word to the end of the word.
  2. Add "ay" to the end of the word.
  3. If the string is a punctuation mark or a number, leave it as is. Leave the cases of the words untouched.


M
ove the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.

Examples:


pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !'); // elloHay orldway !


Simple Pig Latin Java


import java.util.regex.Pattern;


public class PigLatin {
public static String pigIt(String str) {
if(str == null || str.length()==0){
return null;
}
final String []words = str.split("\\s");
final StringBuffer pigLatin = new StringBuffer();
for(String s :words){
if(Pattern.matches("\\p{IsPunctuation}",s)){
pigLatin.append(s).append(" ");
}
else{
pigLatin.append(s.substring(1)).
append(s.charAt(0)).
append("ay ");
}
}
while (pigLatin.charAt(finalWords.length() - 1) == ' ') {
pigLatin.setLength(pigLatin.length() - 1);
}

return new String(pigLatin);
}
}

Simple pig Latin JavaScript


function pigIt(str){
let words = str.split(' ');
let pigLatin = [];

for(let word of words){
if((/([a-zA-Z])/).test(word)){
pigLatin.push(word.substring(1) + word[0] + "ay");
}else{
pigLatin.push(word);
}
}
return pigLatin.join(" ");
}

Simple Pig Latin Python


def pig_it(text):
'''Converts text into pig-latin'''
words = text.split(' ')
pigLatin = ' '
for word in words:
if word in '!.%&?':
pigLatin = pigLatin + word
else:
pig_word = word[1:] + word[0] + 'ay'
pigLatin = pigLatin + pig_word + ' '
return pigLatin.strip(' ')

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !