Skip to content

Commit 81f9296

Browse files
authored
Merge pull request exercism#240 from exercism/sublist
sublist: add to track
2 parents 13b0e59 + 9983369 commit 81f9296

8 files changed

Lines changed: 248 additions & 1 deletion

File tree

‎config.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"all-your-base",
5454
"custom-set",
5555
"wordy",
56-
"palindrome-products"
56+
"palindrome-products",
57+
"sublist"
5758
],
5859
"exercises": [
5960
{
@@ -305,6 +306,11 @@
305306
"slug": "palindrome-products",
306307
"difficulty": 1,
307308
"topics": []
309+
},
310+
{
311+
"slug": "sublist",
312+
"difficulty": 1,
313+
"topics": []
308314
}
309315
],
310316
"deprecated": [

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ include 'series'
4444
include 'sieve'
4545
include 'simple-cipher'
4646
include 'simple-linked-list'
47+
include 'sublist'
4748
include 'sum-of-multiples'
4849
include 'space-age'
4950
include 'strain'

‎exercises/sublist/build.gradle‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
test {
13+
testLogging {
14+
exceptionFormat = 'full'
15+
events = ["passed", "failed", "skipped"]
16+
}
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../main/java/Relationship.java
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.List;
2+
3+
final class RelationshipComputer<T extends Comparable> {
4+
5+
Relationship computeRelationship(final List<T> firstList, final List<T> secondList) {
6+
if (firstList.equals(secondList)) return Relationship.EQUAL;
7+
if (checkIfSublist(firstList, secondList)) return Relationship.SUBLIST;
8+
if (checkIfSublist(secondList, firstList)) return Relationship.SUPERLIST;
9+
return Relationship.UNEQUAL;
10+
}
11+
12+
private boolean checkIfSublist(final List<T> firstList, final List<T> secondList) {
13+
final int firstListSize = firstList.size();
14+
final int secondListSize = secondList.size();
15+
16+
if (firstListSize > secondListSize) return false;
17+
18+
final int numberOfSublistCandidates = secondListSize - firstListSize + 1;
19+
20+
for (int startIndex = 0; startIndex < numberOfSublistCandidates; startIndex++) {
21+
if (secondList.subList(startIndex, startIndex + firstListSize).equals(firstList)) {
22+
return true;
23+
}
24+
}
25+
26+
return false;
27+
}
28+
29+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum Relationship {
2+
3+
EQUAL, SUBLIST, SUPERLIST, UNEQUAL
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
final class RelationshipComputer<T extends Comparable> {
2+
3+
4+
5+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import org.junit.Ignore;
2+
import org.junit.Test;
3+
4+
import java.util.List;
5+
6+
import static java.util.Arrays.asList;
7+
import static java.util.Collections.emptyList;
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class RelationshipComputerTest {
11+
12+
@Test
13+
public void testThatTwoEmptyListsAreConsideredEqual() {
14+
Relationship computedRelationship = new RelationshipComputer<>().computeRelationship(
15+
emptyList(),
16+
emptyList());
17+
18+
assertEquals(Relationship.EQUAL, computedRelationship);
19+
}
20+
21+
@Ignore
22+
@Test
23+
public void testEmptyListIsSublistOfNonEmptyList() {
24+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
25+
emptyList(),
26+
asList(1, 2, 3));
27+
28+
assertEquals(Relationship.SUBLIST, relationship);
29+
}
30+
31+
@Ignore
32+
@Test
33+
public void testNonEmptyListIsSuperlistOfEmptyList() {
34+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
35+
asList('1', '2', '3'),
36+
emptyList());
37+
38+
assertEquals(Relationship.SUPERLIST, relationship);
39+
}
40+
41+
@Ignore
42+
@Test
43+
public void testListIsEqualToItself() {
44+
List<String> anyList = asList("1", "2", "3");
45+
46+
Relationship relationship = new RelationshipComputer<String>().computeRelationship(
47+
anyList,
48+
anyList);
49+
50+
assertEquals(Relationship.EQUAL, relationship);
51+
}
52+
53+
@Ignore
54+
@Test
55+
public void testDifferentListsOfTheSameLengthAreUnequal() {
56+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
57+
asList(1, 2, 3),
58+
asList(2, 3, 4));
59+
60+
assertEquals(Relationship.UNEQUAL, relationship);
61+
}
62+
63+
@Ignore
64+
@Test
65+
public void testSublistCheckDoesNotAbortAfterFalseStart() {
66+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
67+
asList('1', '2', '5'),
68+
asList('0', '1', '2', '3', '1', '2', '5', '6'));
69+
70+
assertEquals(Relationship.SUBLIST, relationship);
71+
}
72+
73+
@Ignore
74+
@Test
75+
public void testSublistCheckHandlesExtraneousRepeatsOfFirstEntry() {
76+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
77+
asList("1", "1", "2"),
78+
asList("0", "1", "1", "1", "2", "1", "2"));
79+
80+
assertEquals(Relationship.SUBLIST, relationship);
81+
}
82+
83+
@Ignore
84+
@Test
85+
public void testSublistAtStart() {
86+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
87+
asList(0, 1, 2),
88+
asList(0, 1, 2, 3, 4, 5));
89+
90+
assertEquals(Relationship.SUBLIST, relationship);
91+
}
92+
93+
@Ignore
94+
@Test
95+
public void testSublistInMiddle() {
96+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
97+
asList('2', '3', '4'),
98+
asList('0', '1', '2', '3', '4', '5'));
99+
100+
assertEquals(Relationship.SUBLIST, relationship);
101+
}
102+
103+
@Ignore
104+
@Test
105+
public void testSublistAtEnd() {
106+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
107+
asList("3", "4", "5"),
108+
asList("0", "1", "2", "3", "4", "5"));
109+
110+
assertEquals(Relationship.SUBLIST, relationship);
111+
}
112+
113+
@Ignore
114+
@Test
115+
public void testAtStartOfSuperlist() {
116+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
117+
asList(0, 1, 2, 3, 4, 5),
118+
asList(0, 1, 2));
119+
120+
assertEquals(Relationship.SUPERLIST, relationship);
121+
}
122+
123+
@Ignore
124+
@Test
125+
public void testInMiddleOfSuperlist() {
126+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
127+
asList('0', '1', '2', '3', '4', '5'),
128+
asList('2', '3'));
129+
130+
assertEquals(Relationship.SUPERLIST, relationship);
131+
}
132+
133+
@Ignore
134+
@Test
135+
public void testAtEndOfSuperlist() {
136+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
137+
asList("0", "1", "2", "3", "4", "5"),
138+
asList("3", "4", "5"));
139+
140+
assertEquals(Relationship.SUPERLIST, relationship);
141+
}
142+
143+
@Ignore
144+
@Test
145+
public void testFirstListMissingElementFromSecondList() {
146+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
147+
asList(1, 3),
148+
asList(1, 2, 3));
149+
150+
assertEquals(Relationship.UNEQUAL, relationship);
151+
}
152+
153+
@Ignore
154+
@Test
155+
public void testSecondListMissingElementFromFirstList() {
156+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
157+
asList('1', '2', '3'),
158+
asList('1', '3'));
159+
160+
assertEquals(Relationship.UNEQUAL, relationship);
161+
}
162+
163+
@Ignore
164+
@Test
165+
public void testThatListOrderingIsAccountedFor() {
166+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
167+
asList("1", "2", "3"),
168+
asList("3", "2", "1"));
169+
170+
assertEquals(Relationship.UNEQUAL, relationship);
171+
}
172+
173+
@Ignore
174+
@Test
175+
public void testThatListsWithSameDigitsButDifferentNumbersAreUnequal() {
176+
Relationship relationship = new RelationshipComputer<>().computeRelationship(
177+
asList(1, 0, 1),
178+
asList(10, 1));
179+
180+
assertEquals(Relationship.UNEQUAL, relationship);
181+
}
182+
183+
}

0 commit comments

Comments
 (0)