Skip to content

Commit c2ca198

Browse files
authored
Merge pull request exercism#1599 from hgvanpariya/series_multilist
series: change return type of 'slices' method exercism#1598
2 parents 86de1c3 + daa0328 commit c2ca198

2 files changed

Lines changed: 33 additions & 36 deletions

File tree

‎exercises/series/.meta/src/reference/java/Series.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ class Series {
1414
this.digitsSize = string.isEmpty()?0:this.digits.size();
1515
}
1616

17-
List<List<String>> slices(int num) {
17+
List<String> slices(int num) {
1818
if (num <= 0) {
1919
throw new IllegalArgumentException("Slice size is too small.");
2020
}
2121
if (num > this.digitsSize) {
2222
throw new IllegalArgumentException("Slice size is too big.");
2323
}
2424
final int limit = this.digitsSize - num + 1;
25-
List<List<String>> result = new ArrayList<>(limit);
25+
List<String> result = new ArrayList<>(limit);
2626
List<String> tmp;
2727
for (int i = 0; i < limit; i++) {
2828
tmp = this.digits.subList(i, i + num);
29-
result.add(tmp);
29+
StringBuilder eachElement = new StringBuilder();
30+
for (String string : tmp) {
31+
eachElement.append(string);
32+
}
33+
result.add(eachElement.toString());
3034
}
3135
return result;
3236
}

‎exercises/series/src/test/java/SeriesTest.java‎

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,74 +19,67 @@ public class SeriesTest {
1919
@Test
2020
public void slicesOfOneFromOne() {
2121
Series series = new Series("1");
22-
List<List<String>> expected = Arrays.asList(Arrays.asList("1"));
23-
List<List<String>> actual = series.slices(1);
22+
List<String> expected = Collections.singletonList("1");
23+
List<String> actual = series.slices(1);
2424
assertEquals(expected, actual);
2525
}
2626

2727
@Ignore("Remove to run test")
2828
@Test
2929
public void slicesOfOneFromTwo() {
3030
Series series = new Series("12");
31-
List<List<String>> expected = Arrays.asList(
32-
Arrays.asList("1"),
33-
Arrays.asList("2")
34-
);
35-
List<List<String>> actual = series.slices(1);
31+
List<String> expected = Arrays.asList("1", "2");
32+
List<String> actual = series.slices(1);
3633
assertEquals(expected, actual);
3734
}
3835

3936
@Ignore("Remove to run test")
4037
@Test
4138
public void slicesOfTwo() {
4239
Series series = new Series("35");
43-
List<List<String>> expected = Arrays.asList(Arrays.asList("3", "5"));
44-
List<List<String>> actual = series.slices(2);
40+
List<String> expected = Collections.singletonList("35");
41+
List<String> actual = series.slices(2);
4542
assertEquals(expected, actual);
4643
}
4744

4845
@Ignore("Remove to run test")
4946
@Test
5047
public void slicesOfTwoOverlap() {
5148
Series series = new Series("9142");
52-
List<List<String>> expected = Arrays.asList(
53-
Arrays.asList("9", "1"),
54-
Arrays.asList("1", "4"),
55-
Arrays.asList("4", "2")
56-
);
57-
List<List<String>> actual = series.slices(2);
49+
List<String> expected = Arrays.asList("91","14","42");
50+
List<String> actual = series.slices(2);
5851
assertEquals(expected, actual);
5952
}
6053

6154
@Ignore("Remove to run test")
6255
@Test
6356
public void slicesIncludeDuplicates() {
6457
Series series = new Series("777777");
65-
List<List<String>> expected = Arrays.asList(
66-
Arrays.asList("7", "7", "7"),
67-
Arrays.asList("7", "7", "7"),
68-
Arrays.asList("7", "7", "7"),
69-
Arrays.asList("7", "7", "7")
58+
List<String> expected = Arrays.asList(
59+
"777",
60+
"777",
61+
"777",
62+
"777"
7063
);
71-
List<List<String>> actual = series.slices(3);
72-
assertEquals(expected, actual);
64+
List<String> actual = series.slices(3);
65+
assertEquals(expected, actual);
7366
}
7467

7568
@Ignore("Remove to run test")
7669
@Test
7770
public void slicesOfLongSeries() {
7871
Series series = new Series("918493904243");
79-
List<List<String>> expected = Arrays.asList(
80-
Arrays.asList("9", "1", "8", "4", "9"),
81-
Arrays.asList("1", "8", "4", "9", "3"),
82-
Arrays.asList("8", "4", "9", "3", "9"),
83-
Arrays.asList("4", "9", "3", "9", "0"),
84-
Arrays.asList("9", "3", "9", "0", "4"),
85-
Arrays.asList("3", "9", "0", "4", "2"),
86-
Arrays.asList("9", "0", "4", "2", "4"),
87-
Arrays.asList("0", "4", "2", "4", "3")
72+
List<String> expected = Arrays.asList(
73+
"91849",
74+
"18493",
75+
"84939",
76+
"49390",
77+
"93904",
78+
"39042",
79+
"90424",
80+
"04243"
8881
);
89-
List<List<String>> actual = series.slices(5);
82+
List<String> actual = series.slices(5);
9083
assertEquals(expected, actual);
9184
}
9285

@@ -124,7 +117,7 @@ public void emptySeries() {
124117
expectedException.expect(IllegalArgumentException.class);
125118
expectedException.expectMessage("Slice size is too big.");
126119
Series series = new Series("");
127-
List<List<String>> slices = series.slices(1);
120+
series.slices(1);
128121
}
129122

130123
}

0 commit comments

Comments
 (0)