Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alphanumeric String Sort; Array Pair Sum; Array Sum - Java solution #121

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
110 changes: 110 additions & 0 deletions problems/alphanumeric-string-sort/alphanumeric-string-sort.java
Original file line number Diff line number Diff line change
@@ -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<Character> 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);
}
}
99 changes: 99 additions & 0 deletions problems/array-pair-sum/array-pair-sum-test-v2.java
Original file line number Diff line number Diff line change
@@ -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<Integer> inputArray = null;
int sum = 10;

// Act
arrayPairSum.finArrayPairSums(sum, inputArray);
}

@Test
public void testArrayPairSum_ShouldReturnEmptyArray_WhenInputArrayIsEmpty() {

// Arrange
ArrayList<Integer> inputArray = new ArrayList<Integer>();
int sum = 10;

// Act
List returnedArray = arrayPairSum.finArrayPairSums(sum, inputArray);

// Assert
assertEquals(inputArray, returnedArray);
}

@Test
public void testArrayPairSum_ShouldReturnPairSums_WhenInputArrayHasValues_Case1() {

// Arrange
ArrayList<Integer> inputArray = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7));
int sum = 10;
ArrayList<ArrayList<Integer>> 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<Integer> inputArray = new ArrayList<>(Arrays.asList(3, 4, 5, 4, 4));
int sum = 8;
ArrayList<ArrayList<Integer>> 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<Integer> inputArray = new ArrayList<>(Arrays.asList(1, 5, 4, 7, 5, 3, 3));
int sum = 8;
ArrayList<ArrayList<Integer>> 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);
}
}
35 changes: 35 additions & 0 deletions problems/array-pair-sum/array-pair-sum-v2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.ArrayList;
import java.util.Arrays;

public class ArrayPairSum {

protected ArrayList<ArrayList<Integer>> finArrayPairSums(int sum, ArrayList<Integer> 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<ArrayList<Integer>> 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;
}
}
14 changes: 14 additions & 0 deletions problems/array-sum/array-sum.java
Original file line number Diff line number Diff line change
@@ -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<Object> flatten(Object[] array) {
return Arrays.stream(array)
.flatMap(o -> o instanceof Object[] ? flatten((Object[]) o) : Stream.of(o));
}
}
Loading