diff --git a/.project b/.project new file mode 100644 index 00000000..8b50e8f3 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Algorithms-and-Data-Structures-in-Java + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000..99f26c02 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/src/main/java/com/hackerrank/algorithms/arraysandsorting/InsertionSort2Test.java b/src/main/java/com/hackerrank/algorithms/arraysandsorting/InsertionSort2Test.java new file mode 100644 index 00000000..1ca40be2 --- /dev/null +++ b/src/main/java/com/hackerrank/algorithms/arraysandsorting/InsertionSort2Test.java @@ -0,0 +1,33 @@ +package InsertionSort; + +import static org.junit.Assert.assertArrayEquals; + +import org.junit.Test; + +public class InsertionSort2Test { + @Test + public void shouldDoNothingWithEmptyArray() { + int[] values = {}; + + InsertionSort2.insertionSortPart2(values); + } + + @Test + public void shouldDoNothingWithOneElementArray() { + int[] values = {16}; + + InsertionSort2.insertionSortPart2(values); + + assertArrayEquals(new int[] {16}, values); + } + + @Test + public void shouldSortValues() { + int[] values = { 11, -4, 3, 0, 1}; + int[] expectedOrder = { -4, 0, 1, 3, 11}; + + InsertionSort2.insertionSortPart2(values); + + assertArrayEquals(expectedOrder, values); + } +} diff --git a/src/main/java/com/hackerrank/algorithms/strings/PalindromeIndexTest.java b/src/main/java/com/hackerrank/algorithms/strings/PalindromeIndexTest.java new file mode 100644 index 00000000..8d876d92 --- /dev/null +++ b/src/main/java/com/hackerrank/algorithms/strings/PalindromeIndexTest.java @@ -0,0 +1,81 @@ +package palimdromeIndex; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PalindromeIndexTest { + + private String input; + + @Before + public void setUp() throws Exception { + + input = null; + + } + + @After + public void tearDown() throws Exception { + + } + + @Test(expected = NullPointerException.class) + public void nullStringTest() throws Exception { + + PalindromeIndex.isPalindrome(null); + + } + + @Test + public void emptyStringTest() throws Exception { + + input = ""; + + assertTrue(PalindromeIndex.isPalindrome(input)); + + } + + @Test + public void singleCharTest() throws Exception { + + input = "H"; + + assertTrue(PalindromeIndex.isPalindrome(input)); + + } + + @Test + public void alphaNumericPalindromeTest() throws Exception { + + input = "1234321"; + + assertTrue(PalindromeIndex.isPalindrome(input)); + } + + @Test + public void validPalindromeTest() throws Exception { + + input = "madam"; + + assertTrue(PalindromeIndex.isPalindrome(input)); + } + + @Test + public void invalidWordPalindromeTest() throws Exception { + + input = "rotators"; + + assertFalse(PalindromeIndex.isPalindrome(input)); + } + + @Test + public void invalidPalindromeTest() throws Exception { + + input = "I am a tester"; + + assertFalse(PalindromeIndex.isPalindrome(input)); + } +} \ No newline at end of file diff --git a/src/main/java/com/hackerrank/algorithms/strings/SentencePalindrome.java b/src/main/java/com/hackerrank/algorithms/strings/SentencePalindrome.java new file mode 100644 index 00000000..22d4272f --- /dev/null +++ b/src/main/java/com/hackerrank/algorithms/strings/SentencePalindrome.java @@ -0,0 +1,42 @@ +package sentencePalindrome; + +public class SentencePalindrome { + static boolean isPalindrome(String str) { + int l = 0; + int h = str.length() - 1; + + // Lowercase string + str = str.toLowerCase(); + + // Compares character until they are equal + while (l <= h) { + + char getAtl = str.charAt(l); + char getAth = str.charAt(h); + + // If there is another symbol in left + // of sentence + if (!(getAtl >= 'a' && getAtl <= 'z')) + l++; + + // If there is another symbol in right + // of sentence + else if (!(getAth >= 'a' && getAth <= 'z')) + h--; + + // If characters are equal + else if (getAtl == getAth) { + l++; + h--; + } + + else + return false; + } + return true; + } + public static void main(String[] args) { + + + } +} diff --git a/src/main/java/com/hackerrank/algorithms/strings/SentencePalindromeTest.java b/src/main/java/com/hackerrank/algorithms/strings/SentencePalindromeTest.java new file mode 100644 index 00000000..031a0763 --- /dev/null +++ b/src/main/java/com/hackerrank/algorithms/strings/SentencePalindromeTest.java @@ -0,0 +1,99 @@ +package sentencePalindrome; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class SentencePalindromeTest { + + private String input; + + @Before + public void setUp() throws Exception { + + input = null; + + } + + @After + public void tearDown() throws Exception { + + } + + @Test(expected = NullPointerException.class) + public void nullStringTest() throws Exception { + + SentencePalindrome.isPalindrome(null); + } + + @Test + public void emptyStringTest() throws Exception { + + input = ""; + + assertTrue(SentencePalindrome.isPalindrome(input)); + + } + + @Test + public void multipleWhiteSpaceTest() throws Exception { + + input = "A Santa at Nasa"; + + assertTrue(SentencePalindrome.isPalindrome(input)); + + } + + @Test + public void singleCharTest() throws Exception { + + input = "H"; + + assertTrue(SentencePalindrome.isPalindrome(input)); + + } + + @Test + public void punctuationTest() throws Exception { + + input = "Eva, can I see bees in a cave?"; + + assertTrue(SentencePalindrome.isPalindrome(input)); + + } + + @Test + public void unicodeTest() throws Exception { + + input = "Step on no pet."; + + assertFalse(SentencePalindrome.isPalindrome(input)); + + } + + @Test + public void alphaNumericPalindromeTest() throws Exception { + + input = "Air 2 an a2ria"; + + assertTrue(SentencePalindrome.isPalindrome(input)); + } + + @Test + public void validPalindromeTest() throws Exception { + + input = "No lemon no melon"; + + assertTrue(SentencePalindrome.isPalindrome(input)); + } + + @Test + public void invalidPalindromeTest() throws Exception { + + input = "I am a tester"; + + assertFalse(SentencePalindrome.isPalindrome(input)); + } +} diff --git a/src/main/java/com/rampatra/sorting/BubbleSort.java b/src/main/java/com/rampatra/sorting/BubbleSort.java index 8560319b..bbf1ea1e 100644 --- a/src/main/java/com/rampatra/sorting/BubbleSort.java +++ b/src/main/java/com/rampatra/sorting/BubbleSort.java @@ -8,42 +8,42 @@ */ public class BubbleSort { - /** - * In bubble sort, we start at the beginning of the array and swap - * the first two elements if the first is greater than the second. - * Then, we go to the next pair, and so on, continuously making sweeps - * of the array until it is sorted. In doing so, the smaller items - * slowly "bubble" up to the beginning of the list and in each inner - * iteration the largest element is sorted. Ergo, the inner loop runs - * until {@code length - i - 1} times. Time complexity: O(n^2). Space - * complexity: O(1), in place. To learn more: {@see https://youtu.be/6Gv8vg0kcHc} - * - * @param ar to be sorted - */ - private static void bubbleSort(int[] ar) { - for (int i = 0; i < ar.length - 1; i++) { - for (int j = 0; j < ar.length - i - 1; j++) { - if (ar[j] > ar[j + 1]) { - swap(ar, j, j + 1); - } - } - } - } + /** + * In bubble sort, we start at the beginning of the array and swap the first two + * elements if the first is greater than the second. Then, we go to the next + * pair, and so on, continuously making sweeps of the array until it is sorted. + * In doing so, the smaller items slowly "bubble" up to the beginning of the + * list and in each inner iteration the largest element is sorted. Ergo, the + * inner loop runs until {@code length - i - 1} times. Time complexity: O(n^2). + * Space complexity: O(1), in place. To learn more: + * {@see https://youtu.be/6Gv8vg0kcHc} + * + * @param ar to be sorted + */ + static void bubbleSort(int[] ar) { + for (int i = 0; i < ar.length - 1; i++) { + for (int j = 0; j < ar.length - i - 1; j++) { + if (ar[j] > ar[j + 1]) { + swap(ar, j, j + 1); + } + } + } + } - private static void swap(int[] ar, int i, int j) { - int temp = ar[i]; - ar[i] = ar[j]; - ar[j] = temp; - } + private static void swap(int[] ar, int i, int j) { + int temp = ar[i]; + ar[i] = ar[j]; + ar[j] = temp; + } - public static void main(String[] args) { - int[] ar = {2, 5, 1, 7, 8}; - System.out.println(Arrays.toString(ar)); - bubbleSort(ar); - System.out.println(Arrays.toString(ar)); - ar = new int[]{7, 5, 1, 7, 8, 0, 23}; - System.out.println(Arrays.toString(ar)); - bubbleSort(ar); - System.out.println(Arrays.toString(ar)); - } + public static void main(String[] args) { + int[] ar = { 2, 5, 1, 7, 8 }; + System.out.println(Arrays.toString(ar)); + bubbleSort(ar); + System.out.println(Arrays.toString(ar)); + ar = new int[] { 7, 5, 1, 7, 8, 0, 23 }; + System.out.println(Arrays.toString(ar)); + bubbleSort(ar); + System.out.println(Arrays.toString(ar)); + } } diff --git a/src/main/java/com/rampatra/sorting/BubbleSortTest.java b/src/main/java/com/rampatra/sorting/BubbleSortTest.java new file mode 100644 index 00000000..12ab03e8 --- /dev/null +++ b/src/main/java/com/rampatra/sorting/BubbleSortTest.java @@ -0,0 +1,32 @@ +package BubbleSort; +import static org.junit.Assert.assertArrayEquals; + +import org.junit.Test; + +public class BubbleSortTest { + @Test + public void shouldDoNothingWithEmptyArray() { + int[] values = {}; + + BubbleSort.bubbleSort(values); + } + + @Test + public void shouldDoNothingWithOneElementArray() { + int[] values = {16}; + + BubbleSort.bubbleSort(values); + + assertArrayEquals(new int[] {16}, values); + } + + @Test + public void shouldSortValues() { + int[] values = { 11, -4, 3, 0, 1}; + int[] expectedOrder = { -4, 0, 1, 3, 11}; + + BubbleSort.bubbleSort(values); + + assertArrayEquals(expectedOrder, values); + } +}