Implementation of zip function #72 (#92)

* #72: First test passing

* #72: Second test passing

* #72: Third test passing

* #72: Added extra assertion

* #72: Fixed wrong test

* #72: Fixed wrong test

* #72: Added fourth test (passing)

* #72: fixed typo in test name

* #72: added fifth test

* #72: fixed test names

* #72: refactored for multiple inputs

* #72: added extra test cases and documentation

* #72: addressed @shekhargulati comments
This commit is contained in:
César Aguilera
2017-06-14 20:41:54 +05:30
committed by Shekhar Gulati
parent 375da9342d
commit 90322a7339
2 changed files with 108 additions and 0 deletions
+54
View File
@@ -33,6 +33,8 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static java.lang.Math.min;
import static java.util.Collections.emptyList;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;
@@ -1334,5 +1336,57 @@ public abstract class Strman {
return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count);
}
/**
* Aggregates the contents of n strings into a single list of tuples.
*
* @param inputs A list of strings.
* @return A list of strings if none of the strings in the input is null or empty.
* An empty list otherwise.
*/
public static List<String> zip(String... inputs) {
if (inputs.length == 1) {
return inputs[0].codePoints()
.mapToObj(Character::toChars)
.map(String::new)
.collect(toList());
}
int minLength = calculateMinLength(inputs);
if (minLength == -1) {
return emptyList();
}
List<String> zipped = new ArrayList<>(minLength);
for (int elementIndex = 0; elementIndex < minLength; ++elementIndex) {
StringBuilder tuple = new StringBuilder();
for (int inputIndex = 0; inputIndex < inputs.length; ++inputIndex) {
tuple.append(inputs[inputIndex].charAt(elementIndex));
}
zipped.add(tuple.toString());
}
return zipped;
}
private static int calculateMinLength(String[] inputs) {
if (inputs.length == 0) {
return -1;
}
int minLength = Integer.MAX_VALUE;
for (String input : inputs) {
if (isNullOrEmpty(input)) {
return -1;
}
minLength = min(minLength, input.length());
}
return minLength;
}
}
+54
View File
@@ -26,15 +26,19 @@
package strman;
import org.hamcrest.collection.IsCollectionWithSize;
import org.hamcrest.collection.IsEmptyCollection;
import org.junit.Ignore;
import org.junit.Test;
import java.util.*;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
import static org.hamcrest.collection.IsArrayWithSize.emptyArray;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.Assert.*;
import static strman.Strman.*;
import static strman.Strman.endsWith;
@@ -1064,4 +1068,54 @@ public class StrmanTest {
assertThat(underscored("FooBarBaz"), equalTo("foo_bar_baz"));
assertThat(underscored(" foo bar baz "), equalTo("foo_bar_baz"));
}
@Test
public void zip_shouldReturnEmptyList_whenNullOrEmptyIsPassedIn() {
assertThat(zip("a", null), is(empty()));
assertThat(zip("", "a"), is(empty()));
}
@Test
public void zip_shouldReturnListOfOneElement_forSimplestValidInput() {
assertThat(zip("a", "b"), equalTo(asList("ab")));
}
@Test
public void zip_shouldReturnExpectedListOfPairs_whenBothInputsHaveSameSize() {
assertThat(zip("abc", "def"), equalTo(asList("ad", "be", "cf")));
assertThat(zip("ABC", "DEF"), equalTo(asList("AD", "BE", "CF")));
}
@Test
public void zip_shouldReturnExpectedListOfPairs_whenFirstInputIsBiggerThanSecond() {
assertThat(zip("abc", "d"), equalTo(asList("ad")));
assertThat(zip("ABCDE", "FGH"), equalTo(asList("AF", "BG", "CH")));
}
@Test
public void zip_shouldReturnExpectedListOfPairs_whenSecondInputIsBiggerThanFirst() {
assertThat(zip("d", "abc"), equalTo(asList("da")));
assertThat(zip("FGH", "ABCDE"), equalTo(asList("FA", "GB", "HC")));
}
@Test
public void zip_shouldReturnExpectedListOfTriplets_whenThreeInputs() {
assertThat(zip("abc", "def", "ghi"), equalTo(asList("adg", "beh", "cfi")));
}
@Test
public void zip_shouldReturnExpectedListOfTuples_whenMoreThanThreeInputs() {
assertThat(zip("abc", "def", "ghi", "123"), equalTo(asList("adg1", "beh2", "cfi3")));
}
@Test
public void zip_shouldReturnEmptyList_whenNotEnoughInputs() {
assertThat(zip(), is(empty()));
}
@Test
public void zip_shouldReturnInputInListForm_whenOnlyOneInput() {
assertThat(zip("zip"), is(equalTo(asList("z", "i", "p"))));
assertThat(zip("z"), is(equalTo(asList("z"))));
}
}