From 08501e8b9991ed4f56bf6e15e18983098b0eea4f Mon Sep 17 00:00:00 2001 From: vinkje Date: Sat, 19 Sep 2020 19:49:14 +0200 Subject: [PATCH 1/3] 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 3a893350feb2f9728f4927ca6b438050d5d6f115 Mon Sep 17 00:00:00 2001 From: vinkje Date: Sun, 18 Oct 2020 20:28:08 +0200 Subject: [PATCH 2/3] =?UTF-8?q?L=C3=B6sung=20Acronym=20Marc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clean-code-challanges/src/main/java/Acronym.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/clean-code-challanges/src/main/java/Acronym.java b/clean-code-challanges/src/main/java/Acronym.java index 5e00939..a6f1eed 100644 --- a/clean-code-challanges/src/main/java/Acronym.java +++ b/clean-code-challanges/src/main/java/Acronym.java @@ -1,3 +1,4 @@ + /** * Convert a phrase to its acronym. * @@ -6,13 +7,22 @@ * Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG). */ class Acronym { + public final String phrase; + private StringBuilder acronym = new StringBuilder(); Acronym(String phrase) { - + this.phrase = phrase; } String get() { - return null; + String[] words = phrase.replaceAll("-", " ").split(" "); + for (String word : words) { + if (!word.isEmpty()) { + word = word.replaceAll("[^a-zA-Z0-9]", ""); + acronym.append(word.substring(0,1)); + } + } + return acronym.toString().toUpperCase(); } } From de5941df519ac8a42e48141af5f41f436e4bb0a9 Mon Sep 17 00:00:00 2001 From: vinkje Date: Sun, 18 Oct 2020 21:03:09 +0200 Subject: [PATCH 3/3] removed PigLatinTranslations Code --- .../src/main/java/PigLatinTranslator.java | 57 +------------------ 1 file changed, 1 insertion(+), 56 deletions(-) diff --git a/clean-code-challanges/src/main/java/PigLatinTranslator.java b/clean-code-challanges/src/main/java/PigLatinTranslator.java index 4ff0ad5..3e7b990 100644 --- a/clean-code-challanges/src/main/java/PigLatinTranslator.java +++ b/clean-code-challanges/src/main/java/PigLatinTranslator.java @@ -1,8 +1,3 @@ -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 +19,6 @@ 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; - } - - 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"; + return null; } }