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
2 changes: 1 addition & 1 deletion clean-code-challanges/src/main/java/Acronym.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Acronym {

Acronym(String phrase) {

// FOOO
}

String get() {
Expand Down
23 changes: 21 additions & 2 deletions clean-code-challanges/src/main/java/Anagram.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -7,11 +10,27 @@
*/
public class Anagram {

public Anagram(String word) {
private final String word;

public Anagram(String word) {
this.word = word;
}

public List<String> match(List<String> candidates) {
return null;
List<String> 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;

}
}
62 changes: 60 additions & 2 deletions clean-code-challanges/src/main/java/PigLatinTranslator.java
Original file line number Diff line number Diff line change
@@ -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").
Expand All @@ -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;
}
}
1 change: 0 additions & 1 deletion clean-code-challanges/src/test/java/AcronymTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import static org.junit.Assert.assertEquals;

@Ignore
public class AcronymTest {

@Test
Expand Down
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

public class AnagramTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@Ignore
public class IsogramCheckerTest {

@Test
Expand Down