Skip to content
Open
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
107 changes: 106 additions & 1 deletion clean-code-challanges/src/main/java/PigLatinTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,118 @@
* Rule 3: If a word starts with a consonant sound followed by "qu", move it to the end of the word, and then add an "ay" sound to the end of the word (e.g. "square" -> "aresquay").
* Rule 4: If a word contains a "y" after a consonant cluster or as the second letter in a two letter word it makes a vowel sound (e.g. "rhythm" -> "ythmrhay", "my" -> "ymay").
*
* Regel 1: Wenn ein Wort mit einem Vokal beginnt, fügen Sie am Ende des Wortes ein „ay“ hinzu. (a, e, i, o, u)
* Bitte beachten Sie, dass „xr“ und „yt“ am Anfang eines Wortes Vokale erzeugen (z. B. „xray“ -> „xrayay“, „yttria“ -> „yttriaay“).
* Regel 2: Wenn ein Wort mit einem Konsonanten beginnt, verschieben Sie es an das Ende des Wortes und fügen Sie dann einen „ay“-Laut am Ende des Wortes hinzu.
* Konsonantenlaute können aus mehreren Konsonanten bestehen, auch bekannt als Konsonantencluster (z. B. „Stuhl“ -> „airchay“).
* Regel 3: Wenn ein Wort mit einem Konsonanten beginnt, gefolgt von „qu“, verschieben Sie es an das Ende des Wortes und fügen Sie dann einen „ay“-Laut an das Ende des Wortes an (z. B. „Quadrat“ -> „aresquay ").
* Regel 4: Wenn ein Wort ein „y“ nach einer Konsonantengruppe oder als zweiten Buchstaben in einem Wort mit zwei Buchstaben enthält, erzeugt es einen Vokal (z. B. „rhythm“ -> „ythmrhay“, „my“ -> „ymay“) ).
*
*
*
* There are a few more rules for edge cases, and there are regional variants too.
*
* See http://en.wikipedia.org/wiki/Pig_latin for more details.
*/
public class PigLatinTranslator {
char[] vowels = {'a','e','i','o','u'};
char[] consonant = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'};
String[] doubelconsonant = {"ch", "qu", "th","rh"};
String[] doubelvowel = {"xr", "yt"};

public String translate(String englishPhrase) {
return null;
char c = englishPhrase.charAt(0);
String result = "";
char[] chars = stringToCharArray(englishPhrase);

if (isFirstCharAvowel(c)||isFirst2CharsAdoubleconsonant(chars,doubelvowel,0)){
result = translateAyToTheEnd(englishPhrase);
}else {
if(isCharAconsonant(chars,0)){
result = translateConsonantAndAYtoEnd(chars);
if(isFirst2CharsAdoubleconsonant(chars,doubelconsonant,0)){
result = translateDoubleConsAndAytoEnd(englishPhrase,chars);
if(isCharAconsonant(chars,2)){
result = translateFirst3charsAndAyToEnd(englishPhrase,chars);
if(chars[2] =='y'){
result = translateDoubleConsAndAytoEnd(englishPhrase,chars);
}
}
}
if (isFirst2CharsAdoubleconsonant(chars,doubelconsonant,1)){
result = translateFirst3charsAndAyToEnd(englishPhrase, chars);
}
if(chars[1] =='y'){
result = translateConsonantAndAYtoEnd(chars);
}
}
}
return result;
}

//Check
private boolean isFirstCharAvowel(char character){
for (char c:vowels) {
if(c == character){
return true;
}
}return false;
}

private boolean isCharAconsonant(char[] chars,int position){
for (int i = position; i < consonant.length; i++) {
if(chars[position] == consonant[i]){
return true;
}
}
return false;
}

private boolean isFirst2CharsAdoubleconsonant(char[] chars, String[] stringarray, int start){
String res = String.valueOf(chars[start])+ String.valueOf(chars[start+1]);
for (String text:stringarray) {
if(res.equals(text)){
return true;
}
}
return false;
}

//Translate
private String translateAyToTheEnd(String text){
return text + "ay";
}
private String translateConsonantAndAYtoEnd(char[] textchars){
String result = "";
for (int j = 1; j < textchars.length; j++) {
result = result + String.valueOf(textchars[j]);
}
return result + textchars[0] + "ay";
}
private String translateDoubleConsAndAytoEnd(String text, char[] textchars){
String result = "";
for (int i = 2; i < textchars.length; i++) {
result = result + String.valueOf(textchars[i]);
}
return result + textchars[0] + textchars[1]+"ay";

}
private String translateFirst3charsAndAyToEnd(String text, char[] textchars){
String result="";
for (int i = 3; i < textchars.length; i++) {
result = result + String.valueOf(textchars[i]);
}
return result + textchars[0] + textchars[1] + textchars[2] + "ay";
}

//Helpers
private char[] stringToCharArray(String text){
char[] result = new char[text.length()];
for (int i = 0; i < text.length(); i++) {
result[i] = text.charAt(i);
}
return result;
}

}

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
//@Ignore
public class AnagramTest {

@Test
Expand Down