From 08501e8b9991ed4f56bf6e15e18983098b0eea4f Mon Sep 17 00:00:00 2001 From: vinkje Date: Sat, 19 Sep 2020 19:49:14 +0200 Subject: [PATCH 1/5] PigLatin implemented --- .../src/main/java/PigLatinTranslator.java | 70 +++++++++++++++++-- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/clean-code-challanges/src/main/java/PigLatinTranslator.java b/clean-code-challanges/src/main/java/PigLatinTranslator.java index 602a04d..4ff0ad5 100644 --- a/clean-code-challanges/src/main/java/PigLatinTranslator.java +++ b/clean-code-challanges/src/main/java/PigLatinTranslator.java @@ -1,23 +1,79 @@ +import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; +import sun.security.util.ArrayUtil; + +import java.util.Arrays; +import java.util.List; + /** * Implement a program that translates from English to Pig Latin. - * + *

* Pig Latin is a made-up children's language that's intended to be confusing. It obeys a few simple rules (below), * but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand. - * + *

* Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word. - * Please note that "xr" and "yt" at the beginning of a word make vowel sounds (e.g. "xray" -> "xrayay", "yttria" -> "yttriaay"). + * Please note that "xr" and "yt" at the beginning of a word make vowel sounds (e.g. "xray" -> "xrayay", "yttria" -> "yttriaay"). * Rule 2: If a word begins with a consonant sound, move it to the end of the word and then add an "ay" sound to the end of the word. - * Consonant sounds can be made up of multiple consonants, a.k.a. a consonant cluster (e.g. "chair" -> "airchay"). + * Consonant sounds can be made up of multiple consonants, a.k.a. a consonant cluster (e.g. "chair" -> "airchay"). * 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"). - * + *

* 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 { public String translate(String englishPhrase) { - return null; + String[] words = spiltAndReplace(englishPhrase); + + System.out.println(followedByQu(words[0])); + + for (int i = 0; i < words.length; i++) { + while (!beginsWithVowel(words[i]) && !followedByQu(words[i]) && !followedByY(words[i])) { + words[i] = moveNCharToEnd(words[i], 1); + } + + if (followedByQu(words[i])) { + words[i] = moveNCharToEnd(words[i], 3); + } + + if (followedByY(words[i])){ + words[i] = moveNCharToEnd(words[i], 1); + } + + words[i] = addAy(words[i]); + } + + return String.join(" ", words); + } + + private String[] spiltAndReplace(String englishPhrase) { + String[] words = englishPhrase.split(" "); + for (int i = 0; i < words.length; i++) { + words[i] = words[i].replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); + } + return words; + } + + public boolean beginsWithVowel(String word) { + List vowelChars = Arrays.asList('a', 'i', 'e', 'o', 'u'); + String[] vowelStrings = {"xr", "yt"}; + return vowelChars.contains(word.charAt(0)) || Arrays.asList(vowelStrings).contains(word.substring(0, 2)); + } + + public boolean followedByQu(String word) { + return word.substring(1, 3).contains("qu"); + } + + public boolean followedByY(String word) { + return word.substring(1,2).contains("y"); + } + + public String moveNCharToEnd(String word, int numberOfChar) { + return word.substring(numberOfChar) + word.substring(0, numberOfChar); + } + + public String addAy(String word) { + return word + "ay"; } } From ee16aa9466732bbfbbfc7a3fa67e6c8cefa5e6e6 Mon Sep 17 00:00:00 2001 From: vinkje Date: Sun, 18 Oct 2020 20:55:47 +0200 Subject: [PATCH 2/5] =?UTF-8?q?L=C3=B6sung=20Anagram=20Marc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/Anagram.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/clean-code-challanges/src/main/java/Anagram.java b/clean-code-challanges/src/main/java/Anagram.java index 9930cd0..0c73ad1 100644 --- a/clean-code-challanges/src/main/java/Anagram.java +++ b/clean-code-challanges/src/main/java/Anagram.java @@ -1,4 +1,6 @@ +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; /** * Given a word and a list of possible anagrams, select the correct sublist. @@ -6,12 +8,27 @@ * Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets". */ public class Anagram { + private final String word; + private final char[] wordChars; public Anagram(String word) { - + this.word = word; + this.wordChars = word.toLowerCase().toCharArray(); + Arrays.sort(wordChars); } public List match(List candidates) { - return null; + List anagrams; + anagrams = candidates.stream() + .filter(this::isAnagram) + .collect(Collectors.toList()); + return anagrams; + } + + private boolean isAnagram(String candidate){ + char[] candidateChars = candidate.toLowerCase().toCharArray(); + Arrays.sort(candidateChars); + + return !word.equalsIgnoreCase(candidate) && Arrays.equals(candidateChars, wordChars); } } From d1db9e9ffd348d68ac8448caab6eea4e64573157 Mon Sep 17 00:00:00 2001 From: vinkje Date: Fri, 23 Oct 2020 09:13:24 +0200 Subject: [PATCH 3/5] ignored piglatin test --- clean-code-challanges/src/test/java/PigLatinTranslatorTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clean-code-challanges/src/test/java/PigLatinTranslatorTest.java b/clean-code-challanges/src/test/java/PigLatinTranslatorTest.java index c26cdcd..89071b9 100644 --- a/clean-code-challanges/src/test/java/PigLatinTranslatorTest.java +++ b/clean-code-challanges/src/test/java/PigLatinTranslatorTest.java @@ -1,3 +1,4 @@ +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -7,6 +8,7 @@ import static org.junit.Assert.assertEquals; +@Ignore @RunWith(Parameterized.class) public class PigLatinTranslatorTest { From c77b6ecee6fd005bad4f605064c56df066b4d9a9 Mon Sep 17 00:00:00 2001 From: vinkje Date: Fri, 23 Oct 2020 09:16:11 +0200 Subject: [PATCH 4/5] activated anagramTest --- clean-code-challanges/src/test/java/AnagramTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clean-code-challanges/src/test/java/AnagramTest.java b/clean-code-challanges/src/test/java/AnagramTest.java index ad4faba..76d97be 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 + public class AnagramTest { @Test From 90355080cfe5376eac571e60f917249230f97bff Mon Sep 17 00:00:00 2001 From: vinkje Date: Fri, 23 Oct 2020 09:19:30 +0200 Subject: [PATCH 5/5] deleted piglatin --- .../src/main/java/PigLatinTranslator.java | 55 +------------------ 1 file changed, 1 insertion(+), 54 deletions(-) diff --git a/clean-code-challanges/src/main/java/PigLatinTranslator.java b/clean-code-challanges/src/main/java/PigLatinTranslator.java index 4ff0ad5..7906b10 100644 --- a/clean-code-challanges/src/main/java/PigLatinTranslator.java +++ b/clean-code-challanges/src/main/java/PigLatinTranslator.java @@ -1,8 +1,4 @@ -import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; -import sun.security.util.ArrayUtil; -import java.util.Arrays; -import java.util.List; /** * Implement a program that translates from English to Pig Latin. @@ -24,56 +20,7 @@ public class PigLatinTranslator { public String translate(String englishPhrase) { - String[] words = spiltAndReplace(englishPhrase); - - System.out.println(followedByQu(words[0])); - - for (int i = 0; i < words.length; i++) { - while (!beginsWithVowel(words[i]) && !followedByQu(words[i]) && !followedByY(words[i])) { - words[i] = moveNCharToEnd(words[i], 1); - } - - if (followedByQu(words[i])) { - words[i] = moveNCharToEnd(words[i], 3); - } - - if (followedByY(words[i])){ - words[i] = moveNCharToEnd(words[i], 1); - } - - words[i] = addAy(words[i]); - } - - return String.join(" ", words); - } - - private String[] spiltAndReplace(String englishPhrase) { - String[] words = englishPhrase.split(" "); - for (int i = 0; i < words.length; i++) { - words[i] = words[i].replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); - } - return words; + return null; } - public boolean beginsWithVowel(String word) { - List vowelChars = Arrays.asList('a', 'i', 'e', 'o', 'u'); - String[] vowelStrings = {"xr", "yt"}; - return vowelChars.contains(word.charAt(0)) || Arrays.asList(vowelStrings).contains(word.substring(0, 2)); - } - - public boolean followedByQu(String word) { - return word.substring(1, 3).contains("qu"); - } - - public boolean followedByY(String word) { - return word.substring(1,2).contains("y"); - } - - public String moveNCharToEnd(String word, int numberOfChar) { - return word.substring(numberOfChar) + word.substring(0, numberOfChar); - } - - public String addAy(String word) { - return word + "ay"; - } }