From ffdd9c83ff556d4cb19a806935e282828a7b96df Mon Sep 17 00:00:00 2001 From: TheMly Date: Tue, 15 Oct 2019 16:30:39 +0100 Subject: [PATCH 1/3] Alphanumeric String Sort - Java solution --- .../alphanumeric-string-sort-test.java | 98 ++++++++++++++++ .../alphanumeric-string-sort.java | 110 ++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 problems/alphanumeric-string-sort/alphanumeric-string-sort-test.java create mode 100644 problems/alphanumeric-string-sort/alphanumeric-string-sort.java diff --git a/problems/alphanumeric-string-sort/alphanumeric-string-sort-test.java b/problems/alphanumeric-string-sort/alphanumeric-string-sort-test.java new file mode 100644 index 00000000..0322e990 --- /dev/null +++ b/problems/alphanumeric-string-sort/alphanumeric-string-sort-test.java @@ -0,0 +1,98 @@ +import org.junit.Before; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class AlphanumericStringSortTest { + + private AlphanumericStringSort alphanumericStringSort; + + @Before + void setUp() { + alphanumericStringSort = new AlphanumericStringSort(); + } + + @Test + void sortAlphaNumericString_ShouldReturnInputString_WhenStringIsNull() { + + // Arrange + String originalString = null; + String expectedString = null; + + // Act + String resultString = alphanumericStringSort.alphaNumericStringSort(originalString); + + // Assert + assertEquals(expectedString, resultString); + } + + @Test + void sortAlphaNumericString_ShouldReturnInputString_WhenStringIsEmpty() { + + // Arrange + String originalString = ""; + String expectedString = ""; + + // Act + String resultString = alphanumericStringSort.alphaNumericStringSort(originalString); + + // Assert + assertEquals(expectedString, resultString); + } + + @Test + void sortAlphaNumericString_Case1() { + + // Arrange + String originalString = "3Lj4h5B"; + String expectedString = "hjBL435"; + + // Act + String resultString = alphanumericStringSort.alphaNumericStringSort(originalString); + + // Assert + assertEquals(expectedString, resultString); + } + + @Test + void sortAlphaNumericString_Case2() { + + // Arrange + String originalString = "3jJW9QKjbG"; + String expectedString = "bjjGJKQW39"; + + // Act + String resultString = alphanumericStringSort.alphaNumericStringSort(originalString); + + // Assert + assertEquals(expectedString, resultString); + } + + @Test + void sortAlphaNumericString_Case3() { + + // Arrange + String originalString = "Sorting0123456789"; + String expectedString = "ginortS0246813579"; + + // Act + String resultString = alphanumericStringSort.alphaNumericStringSort(originalString); + + // Assert + assertEquals(expectedString, resultString); + } + + @Test + void sortAlphaNumericString_Case4() { + + // Arrange + String originalString = "0123456789"; + String expectedString = "0246813579"; + + // Act + String resultString = alphanumericStringSort.alphaNumericStringSort(originalString); + + // Assert + assertEquals(expectedString, resultString); + } +} diff --git a/problems/alphanumeric-string-sort/alphanumeric-string-sort.java b/problems/alphanumeric-string-sort/alphanumeric-string-sort.java new file mode 100644 index 00000000..094f6089 --- /dev/null +++ b/problems/alphanumeric-string-sort/alphanumeric-string-sort.java @@ -0,0 +1,110 @@ +import java.util.ArrayList; +import java.util.stream.Collectors; + +public class AlphanumericStringSort { + + /** + * Method to specifically sort a String that can contain characters and numbers. + * The sorting specification goes like this: + * 1 - All sorted lowercase letters are ahead of uppercase letters. + * 2 - All sorted uppercase letters are ahead of digits. + * 3 - All sorted even digits are ahead of sorted odd digits. + * + * @param stringToBeSorted The string to be sorted. + * + * @return The sorted string. + */ + static String alphaNumericStringSort(String stringToBeSorted) { + + if(stringToBeSorted == null || stringToBeSorted.trim().length() == 0) { + return stringToBeSorted; + } + + // StringBuilder structure of the original String, in order to facilitate operations + StringBuilder sb = new StringBuilder(stringToBeSorted); + + // Structure that will hold the sorted result + ArrayList resultChars = new ArrayList<>(); + + // Index where the char should be inserted in result chars structure + int tempIndex = 0; + + // Adding first char + resultChars.add(0, sb.charAt(0)); + + // Deleting the inserted char from the StringBuilder representation of the original string + sb.deleteCharAt(0); + + // While the StringBuilder has chars to be processed + while (sb.length() != 0) { + + char firstChar = sb.charAt(0); + + for (int j = 0; j < resultChars.size(); j++) { + char secondChar = resultChars.get(j); + + // Get Ascii values + int firstCharAscii = firstChar; + int secondCharAscii = secondChar; + + // If chars have the same 'case', we order them by ascii code, because they are in the same group of chars + if((Character.isLowerCase(firstChar) && Character.isLowerCase(secondChar)) || + (Character.isUpperCase(firstChar) && Character.isUpperCase(secondChar))) { + if(firstCharAscii > secondCharAscii) { + tempIndex = j + 1; + } + // If chars are from different types or they are numbers + } else if (firstCharAscii < secondCharAscii || sortNumbers(firstChar, secondChar)) { + tempIndex = j + 1; + } + } + + // Update sorted list of characters with the new character and its computed position + resultChars.add(tempIndex, firstChar); + sb.deleteCharAt(0); + // Reset temporary index in order to process next chars + tempIndex = 0; + } + return resultChars.stream().map(c -> c.toString()).collect(Collectors.joining()); + } + + /** + * Auxiliar method to sort numbers based on their parity. + * + * @param firstChar The first char + * @param secondChar The second char + * + * @return The flag that tells if we should insert the char a position forward or not + */ + private static boolean sortNumbers(char firstChar, char secondChar) { + + if(Character.isDigit(firstChar) && Character.isDigit(secondChar)) { + int firstCharNumber = Character.getNumericValue(firstChar); + int secondCharNumber = Character.getNumericValue(secondChar); + + // First char even and second char odd + if(firstCharNumber % 2 == 0 && secondCharNumber % 2 != 0) { + return false; + // First char odd and second char even + } else if (firstCharNumber % 2 != 0 && secondCharNumber % 2 == 0) { + return true; + // First char and second char same parity, so we evaluate the values of them + } else { + return firstCharNumber > secondCharNumber; + } + } + return false; + } + + public static void main(String[] args) { + + // Arrange + String originalString = "Lj4h35B"; + String expectedString = "hjBL435"; + + // Act + String resultString = alphaNumericStringSort(originalString); + + System.out.println("Sorted string -> " + resultString); + } +} From 067fa3adba6b1f795c24c9338b2d3f000592f770 Mon Sep 17 00:00:00 2001 From: TheMly Date: Wed, 16 Oct 2019 17:44:22 +0100 Subject: [PATCH 2/3] Array Pair Sum - Java solution --- .../array-pair-sum-test-v2.java | 99 +++++++++++++++++++ .../array-pair-sum/array-pair-sum-v2.java | 35 +++++++ 2 files changed, 134 insertions(+) create mode 100644 problems/array-pair-sum/array-pair-sum-test-v2.java create mode 100644 problems/array-pair-sum/array-pair-sum-v2.java diff --git a/problems/array-pair-sum/array-pair-sum-test-v2.java b/problems/array-pair-sum/array-pair-sum-test-v2.java new file mode 100644 index 00000000..32d5fd65 --- /dev/null +++ b/problems/array-pair-sum/array-pair-sum-test-v2.java @@ -0,0 +1,99 @@ +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ArrayPairSumTest { + + private ArrayPairSum arrayPairSum; + + @Before + public void setUp() { + arrayPairSum = new ArrayPairSum(); + } + + @Test(expected = IllegalArgumentException.class) + public void testArrayPairSum_ShouldThrowException_WhenInputArrayIsNull() { + + // Arrange + ArrayList inputArray = null; + int sum = 10; + + // Act + arrayPairSum.finArrayPairSums(sum, inputArray); + } + + @Test + public void testArrayPairSum_ShouldReturnEmptyArray_WhenInputArrayIsEmpty() { + + // Arrange + ArrayList inputArray = new ArrayList(); + int sum = 10; + + // Act + List returnedArray = arrayPairSum.finArrayPairSums(sum, inputArray); + + // Assert + assertEquals(inputArray, returnedArray); + } + + @Test + public void testArrayPairSum_ShouldReturnPairSums_WhenInputArrayHasValues_Case1() { + + // Arrange + ArrayList inputArray = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7)); + int sum = 10; + ArrayList> expectedArray = new ArrayList<>(); + expectedArray.add(new ArrayList<>(Arrays.asList(3, 7))); + expectedArray.add(new ArrayList<>(Arrays.asList(4, 6))); + + // Act + List returnedArray = arrayPairSum.finArrayPairSums(sum, inputArray); + + // Assert + assertEquals(expectedArray, returnedArray); + } + + @Test + public void testArrayPairSum_ShouldReturnPairSums_WhenInputArrayHasValues_Case2() { + + // Arrange + ArrayList inputArray = new ArrayList<>(Arrays.asList(3, 4, 5, 4, 4)); + int sum = 8; + ArrayList> expectedArray = new ArrayList<>(); + expectedArray.add(new ArrayList<>(Arrays.asList(3, 5))); + expectedArray.add(new ArrayList<>(Arrays.asList(4, 4))); + expectedArray.add(new ArrayList<>(Arrays.asList(4, 4))); + expectedArray.add(new ArrayList<>(Arrays.asList(4, 4))); + + // Act + List returnedArray = arrayPairSum.finArrayPairSums(sum, inputArray); + + // Assert + assertEquals(expectedArray, returnedArray); + } + + @Test + public void testArrayPairSum_ShouldReturnPairSums_WhenInputArrayHasValues_Case3() { + + // Arrange + ArrayList inputArray = new ArrayList<>(Arrays.asList(1, 5, 4, 7, 5, 3, 3)); + int sum = 8; + ArrayList> expectedArray = new ArrayList<>(); + expectedArray.add(new ArrayList<>(Arrays.asList(1, 7))); + expectedArray.add(new ArrayList<>(Arrays.asList(5, 3))); + expectedArray.add(new ArrayList<>(Arrays.asList(5, 3))); + expectedArray.add(new ArrayList<>(Arrays.asList(5, 3))); + expectedArray.add(new ArrayList<>(Arrays.asList(5, 3))); + + // Act + List returnedArray = arrayPairSum.finArrayPairSums(sum, inputArray); + + // Assert + assertEquals(expectedArray, returnedArray); + } +} diff --git a/problems/array-pair-sum/array-pair-sum-v2.java b/problems/array-pair-sum/array-pair-sum-v2.java new file mode 100644 index 00000000..ed9a2b4d --- /dev/null +++ b/problems/array-pair-sum/array-pair-sum-v2.java @@ -0,0 +1,35 @@ +import java.util.ArrayList; +import java.util.Arrays; + +public class ArrayPairSum { + + protected ArrayList> finArrayPairSums(int sum, ArrayList numbersList) { + + // Validations + if (numbersList == null) { + throw new IllegalArgumentException("The input array cannot be null!"); + } + if (numbersList.isEmpty()) { + return new ArrayList<>(); + } + + // Structure to be returned that will hold all the sum pairs + ArrayList> pairSums = new ArrayList(); + + while (numbersList.size() > 1) { + int firstNumber = numbersList.get(0); + for (int i = 1; i < numbersList.size(); i++) { + int secondNumber = numbersList.get(i); + + // We add the pair if their sum is equal to the sum value passed as argument + if (firstNumber + secondNumber == sum) { + pairSums.add(new ArrayList<>(Arrays.asList(firstNumber, secondNumber))); + } + } + + // In the end of the iteration, we remove the first value of the list that was just processed and process again + numbersList.remove(0); + } + return pairSums; + } +} From c0c7591f19cd5f1820b675d72726b5b41b08174e Mon Sep 17 00:00:00 2001 From: TheMly Date: Fri, 18 Oct 2019 10:43:38 +0100 Subject: [PATCH 3/3] Array Sum - Java solution --- problems/array-sum/array-sum.java | 14 +++++++ problems/array-sum/array-sum_test.java | 56 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 problems/array-sum/array-sum.java create mode 100644 problems/array-sum/array-sum_test.java diff --git a/problems/array-sum/array-sum.java b/problems/array-sum/array-sum.java new file mode 100644 index 00000000..62b2c2ef --- /dev/null +++ b/problems/array-sum/array-sum.java @@ -0,0 +1,14 @@ +import java.util.Arrays; +import java.util.stream.Stream; + +public class ArraySum { + + int arraySum(Object[] arr) { + return flatten(arr).mapToInt(o -> (int) o).sum(); + } + + private Stream flatten(Object[] array) { + return Arrays.stream(array) + .flatMap(o -> o instanceof Object[] ? flatten((Object[]) o) : Stream.of(o)); + } +} diff --git a/problems/array-sum/array-sum_test.java b/problems/array-sum/array-sum_test.java new file mode 100644 index 00000000..86d233d4 --- /dev/null +++ b/problems/array-sum/array-sum_test.java @@ -0,0 +1,56 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ArraySumTest { + + private ArraySum arraySumCl; + + @Before + public void setUp() { + arraySumCl = new ArraySum(); + } + + @Test + public void testArraySum_ShouldReturnSumOfAllElements_Case1() { + + // Arrange + Object[] arr = {1, 2, new Object[]{3, 4, new Object[]{5}}}; + int expectedSum = 15; + + // Act + int obtainedSum = arraySumCl.arraySum(arr); + + // Assert + assertEquals(expectedSum, obtainedSum); + } + + @Test + public void testArraySum_ShouldReturnSumOfAllElements_Case2() { + + // Arrange + Object[] arr = {1, 4, 7, 5}; + int expectedSum = 17; + + // Act + int obtainedSum = arraySumCl.arraySum(arr); + + // Assert + assertEquals(expectedSum, obtainedSum); + } + + @Test + public void testArraySum_ShouldReturnSumOfAllElements_Case3() { + + // Arrange + Object[] arr = {1, 4, 7, new Object[]{new Object[]{7, 2, 4}, 3, 4, new Object[]{4, 6, 3, 4}}}; + int expectedSum = 49; + + // Act + int obtainedSum = arraySumCl.arraySum(arr); + + // Assert + assertEquals(expectedSum, obtainedSum); + } +}