Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restliche Lösungen #30

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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 .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions clean-code-challanges/src/main/java/Acronym.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package main.java;

import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
/**
* Convert a phrase to its acronym.
*
* <p>
* Techies love their TLA (Three Letter Acronyms)!
*
* <p>
* Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).
*/
class Acronym {
public class Acronym {

Acronym(String phrase) {
String insert;

public Acronym(String phrase) {
insert = phrase;
}

String get() {
return null;
public String get() {
insert = insert.replaceAll("[-]", " ");
String[] words = insert.replaceAll("[^a-zA-Z ]", "").toUpperCase().split("\\s+");
String acronym = "";
for (int i = 0; i < words.length; i++)
acronym += words[i].charAt(0);
return acronym;
}

}
28 changes: 27 additions & 1 deletion clean-code-challanges/src/main/java/Anagram.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
package main.java;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
* Given a word and a list of possible anagrams, select the correct sublist.
*
* Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".
*/
public class Anagram {

private String word;

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

}
public String sortLetters(String word){
char[] letterArray = word.toLowerCase().toCharArray();
Arrays.sort(letterArray);
String sorted = new String (letterArray);
return sorted;
}

public List<String> match(List<String> candidates) {
return null;
List<String> matchingWords = new ArrayList<>();

String sortedWord = sortLetters(this.word);

for (String word: candidates){
String sortedWordMatchingList = sortLetters(word);
if (sortedWord.equals(sortedWordMatchingList) && !this.word.toLowerCase().equals(word.toLowerCase())){
matchingWords.add(word);
}
}
return matchingWords;
}
}


16 changes: 12 additions & 4 deletions clean-code-challanges/src/main/java/IsogramChecker.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package main.java;

/**
* Determine if a word or phrase is an isogram.
*
Expand All @@ -13,10 +15,16 @@
*
* The word isograms, however, is not an isogram, because the s repeats.
*/
class IsogramChecker {
public class IsogramChecker {

boolean isIsogram(String phrase) {
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
}
public boolean isIsogram(String phrase) {
//throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
//}

if (phrase.length() > 1) {
phrase = phrase.toLowerCase().replaceAll(" ", "").replaceAll("-", "");
return phrase.length() == phrase.chars().distinct().count();
}
return true;
}
}
48 changes: 46 additions & 2 deletions clean-code-challanges/src/main/java/PigLatinTranslator.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package main.java;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Implement a program that translates from English to Pig Latin.
*
Expand All @@ -17,7 +22,46 @@
*/
public class PigLatinTranslator {

public String translate(String englishPhrase) {
return null;
public static String translate (String englishPhrase){
char ch = englishPhrase.charAt(0);

Pattern startWithSqu = Pattern.compile("^squ");
Pattern startWithTh = Pattern.compile("^th");
Pattern startWithThr = Pattern.compile("^thr");
Pattern startWithSch = Pattern.compile("^sch");
Pattern startWithYt = Pattern.compile("^yt");
Pattern startWithXr = Pattern.compile("^xr");

String s;
if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') {
s = englishPhrase + "ay";
} else if (ch=='c') {
s = englishPhrase.substring(2) + "chay";

} else if (ch=='q') {
s = englishPhrase.substring(2) + "quay";

} else if (startWithSqu.matcher(englishPhrase).find()) {
s = englishPhrase.substring(3) + "squay";

} else if (startWithThr.matcher(englishPhrase).find()) {
s = englishPhrase.substring(3) + "thray";

} else if (startWithTh.matcher(englishPhrase).find()) {
s = englishPhrase.substring(2) + "thay";

} else if (startWithSch.matcher(englishPhrase).find()) {
s = englishPhrase.substring(3) + "schay";

} else if (startWithYt.matcher(englishPhrase).find()) {
s = englishPhrase + "ay";

} else if (startWithXr.matcher(englishPhrase).find()) {
s = englishPhrase + "ay";

} else {
s = englishPhrase.substring(1) + englishPhrase.charAt(0) + "ay";
}
return s;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package test.java;

import main.java.PigLatinTranslator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -67,5 +70,4 @@ public PigLatinTranslatorTest(String englishPhrase, String pigLatinTranslation)
public void test() {
assertEquals(pigLatinTranslation, new PigLatinTranslator().translate(englishPhrase));
}

}
11 changes: 11 additions & 0 deletions clean-code.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/clean-code-challanges/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand All @@ -17,5 +18,15 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>