Skip to content

Instantly share code, notes, and snippets.

@do-berry
Last active September 6, 2019 11:01
Show Gist options
  • Select an option

  • Save do-berry/90b7a1aa28568c0282b956989393fe8d to your computer and use it in GitHub Desktop.

Select an option

Save do-berry/90b7a1aa28568c0282b956989393fe8d to your computer and use it in GitHub Desktop.
.
import java.lang.IllegalArgumentException;
import java.util.List;
import java.util.ArrayList;
class Series {
public String series;
public Series(String series) {
this.series = series;
}
public List<String> slices(int n) {
if (n < 1) {
throw new IllegalArgumentException("Slice size is too small.");
} else if (n > series.length()) {
throw new IllegalArgumentException("Slice size is too big.");
}
List<String> result = new ArrayList<>();
int index = 0;
while (index + n - 1 != series.length()) {
result.add(series.substring(index, index + n));
index++;
}
return result;
}
}
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.assertEquals;
public class SeriesTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void slicesOfOneFromOne() {
Series series = new Series("1");
List<String> expected = Collections.singletonList("1");
List<String> actual = series.slices(1);
assertEquals(expected, actual);
}
@Test
public void slicesOfOneFromTwo() {
Series series = new Series("12");
List<String> expected = Arrays.asList("1", "2");
List<String> actual = series.slices(1);
assertEquals(expected, actual);
}
@Test
public void slicesOfTwo() {
Series series = new Series("35");
List<String> expected = Collections.singletonList("35");
List<String> actual = series.slices(2);
assertEquals(expected, actual);
}
@Test
public void slicesOfTwoOverlap() {
Series series = new Series("9142");
List<String> expected = Arrays.asList("91", "14", "42");
List<String> actual = series.slices(2);
assertEquals(expected, actual);
}
@Test
public void slicesIncludeDuplicates() {
Series series = new Series("777777");
List<String> expected = Arrays.asList(
"777",
"777",
"777",
"777"
);
List<String> actual = series.slices(3);
assertEquals(expected, actual);
}
@Test
public void slicesOfLongSeries() {
Series series = new Series("918493904243");
List<String> expected = Arrays.asList(
"91849",
"18493",
"84939",
"49390",
"93904",
"39042",
"90424",
"04243"
);
List<String> actual = series.slices(5);
assertEquals(expected, actual);
}
@Test
public void sliceLengthIsToolarge() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Slice size is too big.");
Series series = new Series("12345");
series.slices(6);
}
@Test
public void sliceLengthZero() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Slice size is too small.");
Series series = new Series("12345");
series.slices(0);
}
@Test
public void sliceLengthNegative() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Slice size is too small.");
Series series = new Series("123");
series.slices(-1);
}
@Test
public void emptySeries() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Slice size is too big.");
Series series = new Series("");
series.slices(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment