Skip to content
Open

test #33

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 116 additions & 1 deletion clean-code-challanges/src/main/java/PigLatinTranslator.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.*;

/**
* Implement a program that translates from English to Pig Latin.
*
Expand All @@ -17,7 +19,120 @@
*/
public class PigLatinTranslator {

private final List<String> vowelSounds = Arrays.asList("a", "e", "i", "o", "u");
private final List<String> consonantSounds = Arrays.asList("b", "c", "d", "f", "g", "j", "k", "l", "m", "n", "p", "q", "s", "t", "v", "x", "z", "h", "r", "w", "y");
private final String ay = "ay";
private final String qu = "qu";
private final String xr = "xr";
private final String yt = "yt";


public String translate(String englishPhrase) {
return null;
List<String> wordsList = transformEnglishPhraseToWords(englishPhrase);
List<String> cleanedWords = cleanWords(wordsList);

return pigify(cleanedWords);
}
private String pigify (List <String> words){
for (String word : words){
if (isFirstLetterVowel(word) || isWordStartingWithXrOrYt(word)){
words.set(words.indexOf(word), appendAy(word));
} else if (isConsonantClusterFollowedByY(word)){
words.set(words.indexOf(word), appendTwoFirstLetterAy(word));
}else if (isConsonantFollowedByQu(word)){
words.set(words.indexOf(word), appendThreeFirstLetterAy(word));
}else if (isConsonantCluster(word)){
words.set(words.indexOf(word), appendTwoFirstLetterAy(word));
}else if (isFirstLetterConsonant(word)){
words.set(words.indexOf(word), appendFirstLetterAy(word));
}
}

return transformWordsToPhrase(words);
}

private List<String> transformEnglishPhraseToWords(String englishPhrase) {
return List.of(englishPhrase.toLowerCase(Locale.ROOT).split("\\s+"));
}

private List<String> cleanWords(List<String> words){
List<String> cleanWords = new ArrayList<>();
for (String word : words){
cleanWords.add(word.replaceAll("[^\\w]", ""));
}

return cleanWords;
}





private boolean isFirstLetterVowel(String word){
String firstLetter = String.valueOf(word.charAt(0));
return vowelSounds.contains(firstLetter);
}

private boolean isWordStartingWithXrOrYt(String word){
String firstTwoLetters = word.substring(0,2);
return firstTwoLetters.equals(xr) || firstTwoLetters.equals(yt);
}

private boolean isFirstLetterConsonant(String word){
String firstLetter = String.valueOf(word.charAt(0));
return consonantSounds.contains(firstLetter);
}

private boolean isConsonantCluster(String word){
String secondLetter = String.valueOf(word.charAt(1));
return isFirstLetterConsonant(word) && consonantSounds.contains(secondLetter);
}


private boolean isConsonantFollowedByQu(String word){
String secondAndThirdLetter = "";
if (isMinLengthThree(word)){
secondAndThirdLetter = word.substring(1,3);
}
return isFirstLetterConsonant(word) && secondAndThirdLetter.equals(qu);
}

private boolean isConsonantClusterFollowedByY(String word){
String thirdLetter = "";
if (isMinLengthThree(word)){
thirdLetter = word.substring(2,3);
}

return isConsonantCluster(word) && thirdLetter.equals("y");
}

private boolean isMinLengthThree(String word){
return word.length() >= 3;
}

private String appendAy(String word){
return word + ay;
}
private String appendFirstLetterAy(String word){
return word.substring(1) + word.charAt(0) + ay;

}
private String appendTwoFirstLetterAy(String word){
return word.substring(2) + word.substring(0,2) + ay;
}
private String appendThreeFirstLetterAy(String word){
return word.substring(3) + word.substring(0,3) + ay;
}

private String transformWordsToPhrase(List <String> words){
return String.join(" ", words);
}





}



2 changes: 1 addition & 1 deletion clean-code-challanges/src/test/java/AcronymTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import static org.junit.Assert.assertEquals;

@Ignore

public class AcronymTest {

@Test
Expand Down
2 changes: 1 addition & 1 deletion clean-code-challanges/src/test/java/AnagramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@Ignore

public class AnagramTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@Ignore

public class IsogramCheckerTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class PigLatinTranslatorTest {
@Parameterized.Parameters(name = "{index}: expected \"{0}\" to translate to the pig latin phrase \"{1}\"")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
// Ay is added to words that start with vowels
// Ay is added to words that start with
{"apple", "appleay"},
{"ear", "earay"},
{"igloo", "iglooay"},
Expand Down