From b1d342f886d28def0f619aecc12a1def521a9c4d Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Sat, 10 Sep 2022 11:43:57 +0200 Subject: [PATCH 1/3] Something stupid --- clean-code-challanges/src/main/java/Acronym.java | 2 +- clean-code-challanges/src/test/java/AcronymTest.java | 1 - clean-code-challanges/src/test/java/AnagramTest.java | 2 +- clean-code-challanges/src/test/java/IsogramCheckerTest.java | 1 - 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/clean-code-challanges/src/main/java/Acronym.java b/clean-code-challanges/src/main/java/Acronym.java index 5e00939..945bea2 100644 --- a/clean-code-challanges/src/main/java/Acronym.java +++ b/clean-code-challanges/src/main/java/Acronym.java @@ -8,7 +8,7 @@ class Acronym { Acronym(String phrase) { - + // FOOO } String get() { diff --git a/clean-code-challanges/src/test/java/AcronymTest.java b/clean-code-challanges/src/test/java/AcronymTest.java index 4fec6c2..558f04b 100644 --- a/clean-code-challanges/src/test/java/AcronymTest.java +++ b/clean-code-challanges/src/test/java/AcronymTest.java @@ -3,7 +3,6 @@ import static org.junit.Assert.assertEquals; -@Ignore public class AcronymTest { @Test 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 diff --git a/clean-code-challanges/src/test/java/IsogramCheckerTest.java b/clean-code-challanges/src/test/java/IsogramCheckerTest.java index e6808f9..c4282de 100644 --- a/clean-code-challanges/src/test/java/IsogramCheckerTest.java +++ b/clean-code-challanges/src/test/java/IsogramCheckerTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -@Ignore public class IsogramCheckerTest { @Test From 2048fdbe491abc8267095948d0cd74c0e19803c4 Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Tue, 13 Sep 2022 20:24:12 +0200 Subject: [PATCH 2/3] Anagram Mission completed --- .../src/main/java/Anagram.java | 23 +++++++++++++++++-- 1 file changed, 21 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..95700f1 100644 --- a/clean-code-challanges/src/main/java/Anagram.java +++ b/clean-code-challanges/src/main/java/Anagram.java @@ -1,4 +1,7 @@ +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Objects; /** * Given a word and a list of possible anagrams, select the correct sublist. @@ -7,11 +10,27 @@ */ public class Anagram { - public Anagram(String word) { + private final String word; + public Anagram(String word) { + this.word = word; } public List match(List candidates) { - return null; + List foundAnagrams = new ArrayList<>(); + char[] wordChars = this.word.toLowerCase().toCharArray(); + + Arrays.sort(wordChars); + + for (String candidate : candidates) { + char[] candidateChars = candidate.toLowerCase().toCharArray(); + Arrays.sort(candidateChars); + if(Arrays.equals(wordChars,candidateChars) && !this.word.equalsIgnoreCase(candidate)){ + foundAnagrams.add(candidate); + } + } + + return foundAnagrams; + } } From 1b92adae4c22358379616dd4e673aa101ee3a144 Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Tue, 13 Sep 2022 22:23:12 +0200 Subject: [PATCH 3/3] =?UTF-8?q?Gebe=20auf=20-->=20Test=2017=20konnte=20nic?= =?UTF-8?q?ht=20gel=C3=B6st=20werden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/PigLatinTranslator.java | 62 ++++++++++++++++++- 1 file changed, 60 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..f04b9d4 100644 --- a/clean-code-challanges/src/main/java/PigLatinTranslator.java +++ b/clean-code-challanges/src/main/java/PigLatinTranslator.java @@ -1,10 +1,12 @@ +import java.util.Arrays; + /** * 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. + * Rule 1: If a word begins with a vowel sound (selbstlaut), 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"). * 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"). @@ -18,6 +20,62 @@ public class PigLatinTranslator { public String translate(String englishPhrase) { - return null; + char[] vowels = {'a','e','i','o','u'}; + String vowelsString = "aeiou"; + String consonants = "bcdfghjklmnpqrstvwxyz"; + String specConsonants = "xy"; + String[] consonantClusters = {"rh", "bl", "br", "ch", "ck", "cl", "cr", "dr", "fl", "fr", "gh", "gl", "gr", "ng", "ph", "pl", "pr", "qu", "sc", "sh", "sk", "sl", "sm", "sn", "sp", "st", "sw", "th", "tr", "tw", "wh", "wr"}; + String[] words = englishPhrase.split("\\s+"); + StringBuilder translated = new StringBuilder(); + + int i = 0; + for (String word:words) { + i++; + // Fullfill rule #1 + if( vowelsString.indexOf(word.charAt(0)) > -1 ){ + word = word + "ay"; + } + + // Fullfill rule #2 && #3 consonants + if( consonants.indexOf(word.charAt(0)) > -1 ){ + word = consonantChecks(0, word, consonants, specConsonants, consonantClusters); + word += "ay"; + } + + translated.append(word); + if(words.length > 1 && i < words.length){ + translated.append(" "); + } + } + + // Build translated String + return translated.toString(); + } + + private String consonantChecks(int level, String word, String consonants, String specConsonants, String[] cluster){ + level++; + if(level > 3){ + return word; + } + if(Arrays.asList(cluster).contains(word.substring(0,2).toLowerCase())) { + String clusterToMove = word.substring(0, 2); + word = word.substring(2) + clusterToMove; + } else if (word.length() > 2) { + // rule #4 + if(consonants.indexOf(word.charAt(1)) > -1 && !Arrays.asList(cluster).contains(word.substring(1,3).toLowerCase())) { + return word; + } + } + + if(consonants.indexOf(word.charAt(0)) > -1){ + String charToMove = word.substring(0, 1); + word = word.substring(1) + charToMove; + } + + if(Arrays.asList(cluster).contains(word.substring(0,2).toLowerCase()) || consonants.indexOf(word.charAt(0)) > -1){ + word = consonantChecks(level, word, consonants, specConsonants, cluster); + } + + return word; } }