def simple_pig_latin(input, sep= \" \", end= \"middot\"): Accept a string input,
ID: 3805391 • Letter: D
Question
def simple_pig_latin(input, sep= " ", end= "middot"): Accept a string input, which might include multiple words separated by a separator sep and perform the following steps: Find the list of "words". Inspect every "word" and apply the following conversions: if the word starts with a non-letter or contains no characters, do nothing to it if the word starts with a vowel, add 'way' to the end if the word starts with a consonant place the first letter at the end and add 'ay' Reassemble the words back into one string and return it making sure a single sep is padded between any two neighboring words and the final string ends with end. Assume; the input does not have any capital letters Go ahead and use string methods like. join(), .split () def replace(xs, old, new, Unit=None): Given a list xs, replace occurrences of old value with new value. An optional fourth argument limit is an integer states the maximum number of replacements allowed. You should only replace up to the first limit occurrences of old and leave the rest unchanged. When limit==None, there's truly no limit (and we replace all occurrences of old). Negative or zero limit: no replacement. Assume: xs is a list; xs could be empty. Return None, because the replacement happened in-place.Explanation / Answer
public class string_pattern {
public static void main(String[] args) {
String output = simple_pig_latin("i like this 21312"); // default
// sep=(space),
// end = dot
System.out.println(output);
output = simple_pig_latin("i like this", "."); // sep= dot, end = dot
System.out.println(output);
output = simple_pig_latin("i-like-this", "-"); // sep= -, end = dot
System.out.println(output);
output = simple_pig_latin("i.like.this", ".", "!");// sep= dot, end = !
System.out.println(output);
output = simple_pig_latin("."); // no characters ,end = dot
System.out.println(output);
output = simple_pig_latin("i#like#this", "#", "%");// sep= dot, end = !
System.out.println(output);
}
public static boolean isVowel(char c) {
String vowels = "aeiouAEIOU";
for (int index = 0; index < vowels.length(); index += 1)
if (vowels.charAt(index) == c)
return true;
return false;
}
public static boolean isCons(char c) {
String consonants = "bcdfghjklmnpqrstvwxyz";
for (int index = 0; index < consonants.length(); index += 1)
if (consonants.charAt(index) == c)
return true;
return false;
}
static String simple_pig_latin(String input) {
String sep = " ";
String end = ".";
String[] inputarry = { input };
if (input.contains(sep)) {
inputarry = input.split(sep);
}
StringBuffer stringbuffer = new StringBuffer();
for (String s : inputarry) {
if (isVowel(s.charAt(0))) {
s = s + "way" + sep;
} else if (isCons(s.charAt(0))) {
s = s.substring(1) + s.substring(0, 1) + "way" + sep;
} else {
s = s + sep;
}
stringbuffer.append(s);
}
String output = stringbuffer.toString();
output = output.substring(0, output.lastIndexOf(sep)) + end;
return output;
}
static String simple_pig_latin(String input, String sep) {
String end = ".";
String[] inputarry = { input };
if (input.contains(sep)) {
inputarry = input.split(sep);
}
StringBuffer stringbuffer = new StringBuffer();
for (String s : inputarry) {
if (isVowel(s.charAt(0))) {
s = s + "way" + sep;
} else if (isCons(s.charAt(0))) {
s = s.substring(1) + s.substring(0, 1) + "way" + sep;
} else {
s = s + sep;
}
stringbuffer.append(s);
}
String output = stringbuffer.toString();
output = output.substring(0, output.lastIndexOf(sep)) + end;
return output;
}
static String simple_pig_latin(String input, String sep, String end) {
{
String[] inputarry = { input };
if (input.contains(sep)) {
if (sep == ".")
inputarry = input.split("\.");
else
inputarry = input.split(sep);
}
StringBuffer stringbuffer = new StringBuffer();
for (String s : inputarry) {
if (isVowel(s.charAt(0))) {
s = s + "way" + sep;
} else if (isCons(s.charAt(0))) {
s = s.substring(1) + s.substring(0, 1) + "way" + sep;
} else {
s = s + sep;
}
stringbuffer.append(s);
}
String output = stringbuffer.toString();
output = output.substring(0, output.lastIndexOf(sep)) + end;
return output;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.