diff --git a/README.md b/README.md index ccad873..70b4420 100644 --- a/README.md +++ b/README.md @@ -287,7 +287,7 @@



-## `getNextToLastElement(array)` +## `getSecondToLastElement(array)` * **Description** * Given an array of `String` objects, return the next-to-last element of the array. @@ -299,7 +299,7 @@ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; // : When - boolean outcome = StringArrayUtils.getNextToLastElement(array); + boolean outcome = StringArrayUtils.getSecondToLastElement(array); // : Then System.out.println(outcome); @@ -912,9 +912,9 @@



-## `packDuplicates(array)` +## `packConsecutiveDuplicates(array)` * **Description** - * Given an array of `char` objects, return an array of Strings with conseuctive duplicates placed in an array. + * Given an array of `char` objects, return an array of Strings with consecutive duplicates placed in an array. ### Example 1 @@ -922,10 +922,10 @@ ``` // : Given - char[] array = {'a' 'a' 'a' 'a' 'b' 'c' 'c' 'a' 'a' 'd'} + String[] array = {"a", "a", "a", "a", "b", "c", "c", "a", "a", "d"}; // : When - String[] actual = StringArrayUtils.packDuplicates(array); + String[] actual = StringArrayUtils.packConsecutiveDuplicates(array); // : Then System.out.println(Arrays.toString(actual)); @@ -950,10 +950,10 @@ ``` // : Given - char[] array = {'t', 't', 'q', 'a' 'a' 'a' 'a' 'b'} + String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"}; // : When - String[] actual = StringArrayUtils.packDuplicates(array); + String[] actual = StringArrayUtils.packConsecutiveDuplicates(array); // : Then System.out.println(Arrays.toString(actual)); @@ -976,10 +976,10 @@ ``` // : Given - char[] array = {'m', 'o', 'o', 'n' 'm' 'a' 'n'} + String[] array = {"m", "o", "o", "n", "m", "a", "n"} // : When - String[] actual = StringArrayUtils.packDuplicates(array); + String[] actual = StringArrayUtils.packConsecutiveDuplicates(array); // : Then System.out.println(Arrays.toString(actual)); diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 33cab12..b8cbfac 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -5,31 +5,103 @@ */ public class StringArrayUtils { /** - * @param array - array of String objects - * @param value - value to check array for - * @return - number of occurrences the specified `value` has occurred + * @param array array of String objects + * @return first element of specified array */ // TODO - public static int getNumberOfOccurrences(String[] array, String value) { - return 0; + public static String getFirstElement(String[] array) { + return null; } /** - * @param array - array of String objects - * @param value - value to check array for - * @return - true if the array contains the specified `value` + * @param array array of String objects + * @return second element in specified array */ + public static String getSecondElement(String[] array) { + return null; + } + + /** + * @param array array of String objects + * @return last element in specified array + */ // TODO + public static String getLastElement(String[] array) { + return null; + } + + /** + * @param array array of String objects + * @return second to last element in specified array + */ // TODO + public static String getSecondToLastElement(String[] array) { + return null; + } + + /** + * @param array array of String objects + * @param value value to check array for + * @return true if the array contains the specified `value` + */ // TODO public static boolean contains(String[] array, String value) { return false; } + /** + * @param array of String objects + * @return an array with identical contents in reverse order + */ // TODO + public static String[] reverse(String[] array) { + return null; + } /** - * @param array - array of String objects - * @param valueToRemove - value to remove from array - * @return - array with identical contents excluding values of `value` - */ + * @param array array of String objects + * @return true if the order of the array is the same backwards and forwards + */ // TODO + public static boolean isPalindromic(String[] array) { + return false; + } + /** + * @param array array of String objects + * @return true if each letter in the alphabet has been used in the array + */ // TODO + public static boolean isPangramic(String[] array) { + return false; + } + + /** + * @param array array of String objects + * @param value value to check array for + * @return number of occurrences the specified `value` has occurred + */ // TODO + public static int getNumberOfOccurrences(String[] array, String value) { + return 0; + } + + /** + * @param array array of String objects + * @param valueToRemove value to remove from array + * @return array with identical contents excluding values of `value` + */ // TODO public static String[] removeValue(String[] array, String valueToRemove) { return null; } + + /** + * @param array array of chars + * @return array of Strings with consecutive duplicates removes + */ // TODO + public static String[] removeConsecutiveDuplicates(String[] array) { + return null; + } + + /** + * @param array array of chars + * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings + */ // TODO + public static String[] packConsecutiveDuplicates(String[] array) { + return null; + } + + } diff --git a/src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java b/src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java index 287b11b..4659a62 100644 --- a/src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java +++ b/src/test/java/com/zipcodewilmington/StringArrayUtilsTest.java @@ -7,48 +7,429 @@ * Created by leon on 1/29/18. */ public class StringArrayUtilsTest { - private static final String[] testArray = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + + @Test + public void testGetFirstElement1() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String expected = "the"; + String actual = StringArrayUtils.getFirstElement(array); + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetFirstElement2() { + String[] array = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String expected = "quick"; + String actual = StringArrayUtils.getFirstElement(array); + Assert.assertEquals(expected, actual); + } + + + @Test + public void testGetFirstElement3() { + String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String expected = "brown"; + String actual = StringArrayUtils.getFirstElement(array); + Assert.assertEquals(expected, actual); + } + + + + + + + + + + + + + + + @Test + public void testGetSecondElement1() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String expected = "quick"; + String actual = StringArrayUtils.getSecondElement(array); + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetSecondElement2() { + String[] array = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String expected = "brown"; + String actual = StringArrayUtils.getSecondElement(array); + Assert.assertEquals(expected, actual); + } + + + @Test + public void testGetSecondElement3() { + String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String expected = "fox"; + String actual = StringArrayUtils.getSecondElement(array); + Assert.assertEquals(expected, actual); + } + + + + + + + + + + + + @Test + public void testGetLastElement1() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String expected = "dog"; + String actual = StringArrayUtils.getLastElement(array); + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetLastElement2() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy"}; + String expected = "lazy"; + String actual = StringArrayUtils.getLastElement(array); + Assert.assertEquals(expected, actual); + } + + + @Test + public void testGetLastElement3() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over"}; + String expected = "over"; + String actual = StringArrayUtils.getLastElement(array); + Assert.assertEquals(expected, actual); + } + + + + + + + + + + + + + + + + + + + + + + @Test + public void testGetSecondToLastElement1() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String expected = "lazy"; + String actual = StringArrayUtils.getSecondToLastElement(array); + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetSecondToLastElement2() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "lazy"}; + String expected = "over"; + String actual = StringArrayUtils.getSecondToLastElement(array); + Assert.assertEquals(expected, actual); + } + + + @Test + public void testGetSecondToLastElement3() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over"}; + String expected = "jumps"; + String actual = StringArrayUtils.getSecondToLastElement(array); + Assert.assertEquals(expected, actual); + } + + + + + + + + + + + + + + + + @Test public void testContains() { - for (String s : testArray) { - boolean outcome = StringArrayUtils.contains(testArray, s); + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + for (String s : array) { + boolean outcome = StringArrayUtils.contains(array, s); Assert.assertTrue(outcome); } } + + + + + + + + + + + + @Test - public void testRemoveValue() { - String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; - String[] actual = StringArrayUtils.removeValue(expected, "The"); + public void testReverse1() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String[] expected = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"}; + String[] actual = StringArrayUtils.reverse(array); Assert.assertEquals(expected, actual); } + @Test - public void testRemoveValue1() { - String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; - String[] actual = StringArrayUtils.removeValue(expected, "quick"); + public void testReverse2() { + String[] array = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"}; + String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String[] actual = StringArrayUtils.reverse(array); Assert.assertEquals(expected, actual); } + @Test - public void testRemoveValue2() { - String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"}; - String[] actual = StringArrayUtils.removeValue(expected, "brown"); + public void testReverse3() { + String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String[] actual = StringArrayUtils.reverse(StringArrayUtils.reverse(expected)); Assert.assertEquals(expected, actual); } + + + + + + + + + + @Test + public void testIsPalindromic1() { + String[] array = {"a", "b", "c", "b", "a"}; + boolean outcome = StringArrayUtils.isPalindromic(array); + Assert.assertTrue(outcome); + } + + + + @Test + public void testIsPalindromic2() { + String[] array = {"Is this a plaindrome?", "This is a plaindrome", "Is this a palindrome?"}; + boolean outcome = StringArrayUtils.isPalindromic(array); + Assert.assertTrue(outcome); + } + + + @Test + public void testIsPalindromic3() { + String[] array = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"}; + boolean outcome = StringArrayUtils.isPalindromic(array); + Assert.assertTrue(outcome); + } + + + + + + + + + + @Test + public void testIsPangramic1() { + String[] array = {"The quick brown", "Fox jumps over", "The lazy dog"}; + boolean outcome = StringArrayUtils.isPangramic(array); + Assert.assertTrue(outcome); + } + + @Test + public void testIsPangramic2() { + String[] array = {"The", "quick", "onyx", "goblin", "jumps", "over", "the", "lazy", "dwarf"}; + boolean outcome = StringArrayUtils.isPangramic(array); + Assert.assertTrue(outcome); + } + + @Test + public void testIsPangramic3() { + String[] array = {"Five quacking", "zephyrs", "jolt my", "wax bed"}; + boolean outcome = StringArrayUtils.isPangramic(array); + Assert.assertTrue(outcome); + } + + + + + + + + + + + + + @Test public void testGetNumberOfOccurrences1() { + String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"}; + int expected = 4; + int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba"); + Assert.assertEquals(actual, expected); + } + + @Test + public void testGetNumberOfOccurrences2() { + String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"}; int expected = 2; - int actual = StringArrayUtils.getNumberOfOccurrences(testArray, "the"); + int actual = StringArrayUtils.getNumberOfOccurrences(array, "bbb"); + Assert.assertEquals(actual, expected); + } + + @Test + public void testGetNumberOfOccurrences3() { + String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"}; + int expected = 4; + int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba"); + Assert.assertEquals(actual, expected); + } + + + + + + + + + + + + + + + @Test + public void testRemoveConsecutiveDuplicates1() { + String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"}; + String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array); + String[] expected = {"aba", "baa", "bab", "bba", "bbb"}; + Assert.assertEquals(actual, expected); + } + + + + @Test + public void testRemoveConsecutiveDuplicates2() { + String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"}; + String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array); + String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "bbb"}; + Assert.assertEquals(actual, expected); + } + + + + @Test + public void testRemoveConsecutiveDuplicates3() { + String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb", "aba", "bbb"}; + String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array); + String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"}; + Assert.assertEquals(actual, expected); + } + + + + + + + + + + + + @Test + public void testRemovePackDuplicates1() { + String[] array = {"a", "a", "a", "a", "b", "c", "c", "a", "a", "d"}; + String[] expected = {"aaa", "b", "cc", "aa", "d", "eee"}; + String[] actual = StringArrayUtils.packConsecutiveDuplicates(array); Assert.assertEquals(expected, actual); } + + @Test - public void testGetNumberOfOccurrences2() { - int expected = 1; - int actual = StringArrayUtils.getNumberOfOccurrences(testArray, "quick"); + public void testRemovePackDuplicates2() { + String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"}; + String[] expected = {"tt", "q", "aaa", "cc", "aa", "d", "eee"}; + String[] actual = StringArrayUtils.packConsecutiveDuplicates(array); + Assert.assertEquals(expected, actual); + } + + + + @Test + public void testRemovePackDuplicates3() { + String[] array = {"m", "o", "o", "n", "m", "a", "n"}; + String[] expected = {"m", "oo", "n", "m", "a", "n"}; + String[] actual = StringArrayUtils.packConsecutiveDuplicates(array); + Assert.assertEquals(expected, actual); + } + + + + + + + + + @Test + public void testRemoveValue() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String[] actual = StringArrayUtils.removeValue(array, "The"); + Assert.assertEquals(expected, actual); + } + + @Test + public void testRemoveValue1() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String[] actual = StringArrayUtils.removeValue(array, "quick"); + Assert.assertEquals(expected, actual); + } + + @Test + public void testRemoveValue2() { + String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; + String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"}; + String[] actual = StringArrayUtils.removeValue(array, "brown"); Assert.assertEquals(expected, actual); } + + + + + + + + + + + + }