Skip to content

Commit 9f853e8

Browse files
committed
AcronymMaker
1 parent 64dacbe commit 9f853e8

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.shekhargulati.codegolf;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static java.util.stream.Collectors.joining;
7+
8+
/**
9+
* Your goal is to make a program that converts an input to its acronym.
10+
* Your input is guaranteed to have only letters and spaces. The input will have exactly one space between words.
11+
* You must output the acronym of the input.
12+
*/
13+
public class AcronymMaker {
14+
15+
private static final List<String> STOP_WORDS = Arrays.asList("and", "or", "by", "of");
16+
17+
public static String acronym(final String input) {
18+
/*
19+
Algorithm:
20+
1. Split string into words
21+
2. Filter out all the stopwords
22+
3. Iterate over all the remaining words and take the first char and upper case it
23+
*/
24+
return Arrays.stream(input.toLowerCase().split("\\s")).filter(word -> !STOP_WORDS.contains(word)).map(w -> String.valueOf(w.charAt(0)).toUpperCase()).collect(joining(""));
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.shekhargulati.tadm.ch04.examples;
2+
3+
import java.util.Arrays;
4+
5+
public class MergeSort {
6+
7+
public static void main(String[] args) {
8+
System.out.println(Arrays.toString(mergeSort(new int[]{3, 2, 1, 4})));
9+
}
10+
11+
public static int[] mergeSort(int[] arr) {
12+
if (arr.length == 1) {
13+
return arr;
14+
}
15+
int middle = arr.length / 2;
16+
int[] left = mergeSort(Arrays.copyOfRange(arr, 0, middle));
17+
int[] right = mergeSort(Arrays.copyOfRange(arr, middle, arr.length));
18+
return merge(left, right, arr.length);
19+
}
20+
21+
private static int[] merge(int[] left, int[] right, int length) {
22+
int[] sorted = new int[length];
23+
int[] smaller = ((left.length < right.length) ? left : right);
24+
for (int i = 0; i < smaller.length; i++) {
25+
if (left[i] <= right[i]) {
26+
sorted[i] = left[i];
27+
} else {
28+
sorted[i] = right[i];
29+
}
30+
}
31+
int[] greater = ((left.length > right.length) ? left : right);
32+
for (int i = smaller.length; i < greater.length; i++) {
33+
sorted[i] = greater[i];
34+
}
35+
return sorted;
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.shekhargulati.codegolf;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.equalTo;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class AcronymMakerTest {
9+
10+
@Test
11+
public void acronymOfUnitedStatesOfAmericaIsUSA() throws Exception {
12+
final String input = "United States of America";
13+
final String acronym = AcronymMaker.acronym(input);
14+
assertThat(acronym, equalTo("USA"));
15+
}
16+
17+
@Test
18+
public void acronymOfUnitedStatesOfAmericaIsUSA_lowercase() throws Exception {
19+
final String input = "united states of america";
20+
final String acronym = AcronymMaker.acronym(input);
21+
assertThat(acronym, equalTo("USA"));
22+
}
23+
24+
@Test
25+
public void acronymOfLightAmplificationByStimulationOfEmittedRadiationIsLASER() throws Exception {
26+
final String input = "Light Amplification by Stimulation of Emitted Radiation";
27+
final String acronym = AcronymMaker.acronym(input);
28+
assertThat(acronym, equalTo("LASER"));
29+
}
30+
31+
@Test
32+
public void acronymOfJordanOfTheWorldIsJTW() throws Exception {
33+
final String input = "Jordan Of the World";
34+
final String acronym = AcronymMaker.acronym(input);
35+
assertThat(acronym, equalTo("JTW"));
36+
}
37+
}

0 commit comments

Comments
 (0)