Skip to content

Commit ed69cf8

Browse files
committed
Add new exercise acronym
1 parent 39488b7 commit ed69cf8

10 files changed

Lines changed: 336 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"space-age",
2020
"gigasecond",
2121
"triangle",
22+
"acronym",
2223
"scrabble-score",
2324
"roman-numerals",
2425
"binary",

‎exercises/acronym/build.gradle‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.10"
11+
}
49.8 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Sat Jan 30 17:56:08 EST 2016
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-bin.zip

‎exercises/acronym/gradlew‎

Lines changed: 164 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎exercises/acronym/gradlew.bat‎

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.regex.Matcher;
2+
import java.util.regex.Pattern;
3+
4+
public class Acronym {
5+
public static String generate(String phrase){
6+
final Pattern BREAK_WORDS = Pattern.compile("[A-Z]+[a-z]*|[a-z]+");
7+
final Matcher matcher = BREAK_WORDS.matcher(phrase);
8+
final StringBuilder b = new StringBuilder();
9+
while (matcher.find()){
10+
b.append(matcher.group().charAt(0));
11+
}
12+
return b.toString().toUpperCase();
13+
}
14+
}

‎exercises/acronym/src/main/java/.keep‎

Whitespace-only changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import org.junit.Test;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
public class AcronymTest {
6+
7+
@Test
8+
public void fromTitleCasedPhrases() {
9+
final String phrase = "Portable Network Graphics";
10+
final String expected = "PNG";
11+
assertEquals(expected, Acronym.generate(phrase));
12+
}
13+
14+
@Test
15+
public void fromOtherTitleCasedPhrases() {
16+
final String phrase = "Ruby on Rails";
17+
final String expected = "ROR";
18+
assertEquals(expected, Acronym.generate(phrase));
19+
}
20+
21+
@Test
22+
public void fromInconsistentlyCasedPhrases() {
23+
final String phrase = "HyperText Markup Language";
24+
final String expected = "HTML";
25+
assertEquals(expected, Acronym.generate(phrase));
26+
}
27+
28+
@Test
29+
public void fromPhrasesWithPunctuation() {
30+
final String phrase = "First In, First Out";
31+
final String expected = "FIFO";
32+
assertEquals(expected, Acronym.generate(phrase));
33+
}
34+
35+
@Test
36+
public void fromOtherPhrasesWithPunctuation() {
37+
final String phrase = "PHP: Hypertext Preprocessor";
38+
final String expected = "PHP";
39+
assertEquals(expected, Acronym.generate(phrase));
40+
}
41+
42+
@Test
43+
public void fromPhrasesWithPunctuationAndSentenceCasing() {
44+
final String phrase = "Complementary metal-oxide semiconductor";
45+
final String expected = "CMOS";
46+
assertEquals(expected, Acronym.generate(phrase));
47+
}
48+
49+
}

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include 'accumulate'
2+
include 'acronym'
23
include 'allergies'
34
include 'anagram'
45
include 'atbash-cipher'

0 commit comments

Comments
 (0)