From 6d33c1b06f96d4ff1a6dc0663479305ec651509d Mon Sep 17 00:00:00 2001 From: MitjaPerko <89871456+MitjaPerko@users.noreply.github.com> Date: Tue, 13 Sep 2022 19:11:37 +0200 Subject: [PATCH] First Results, not all Test's correct. Need some Help --- .../src/main/java/PigLatinTranslator.java | 107 +++++++++++++++++- .../src/test/java/AnagramTest.java | 2 +- 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/clean-code-challanges/src/main/java/PigLatinTranslator.java b/clean-code-challanges/src/main/java/PigLatinTranslator.java index 602a04d..dc3270c 100644 --- a/clean-code-challanges/src/main/java/PigLatinTranslator.java +++ b/clean-code-challanges/src/main/java/PigLatinTranslator.java @@ -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; + } + } + diff --git a/clean-code-challanges/src/test/java/AnagramTest.java b/clean-code-challanges/src/test/java/AnagramTest.java index ad4faba..bb5c84c 100644 --- a/clean-code-challanges/src/test/java/AnagramTest.java +++ b/clean-code-challanges/src/test/java/AnagramTest.java @@ -10,7 +10,7 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -@Ignore +//@Ignore public class AnagramTest { @Test