Skip to content

Commit 624d2e7

Browse files
jssanderFridaTveit
authored andcommitted
dominoes: add new exercise (exercism#1379)
* dominoes: add new exercise * dominoes: add new exercise * dominoes: add new exercise * dominoes: add new exercise * dominoes: add new exercise * dominoes: add new exercise * dominoes: add new exercise * dominoes: add new exercise
1 parent 5411aca commit 624d2e7

11 files changed

Lines changed: 430 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,19 @@
11021102
"unlocked_by": "scrabble-score",
11031103
"uuid": "2e760ae2-fadd-4d31-9639-c4554e2826e9"
11041104
},
1105+
{
1106+
"core": false,
1107+
"difficulty": 7,
1108+
"slug": "dominoes",
1109+
"topics": [
1110+
"games",
1111+
"algorithms",
1112+
"lists",
1113+
"exception_handling"
1114+
],
1115+
"unlocked_by": "scrabble-score",
1116+
"uuid": "8e3cb20e-623b-4b4d-8a91-d1a51c0911b5"
1117+
},
11051118
{
11061119
"core": false,
11071120
"difficulty": 8,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ChainNotFoundException extends Exception {
2+
public ChainNotFoundException(String message) {
3+
super(message);
4+
}
5+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Objects;
2+
3+
class Domino {
4+
private int left;
5+
private int right;
6+
Domino(int left, int right) {
7+
this.left = left;
8+
this.right = right;
9+
}
10+
11+
int getLeft() {
12+
return this.left;
13+
}
14+
15+
int getRight() {
16+
return this.right;
17+
}
18+
19+
@Override
20+
public boolean equals(Object o) {
21+
Domino otherDomino = (Domino) o;
22+
return (this.getLeft() == otherDomino.getLeft() && this.getRight() == otherDomino.getRight()) ||
23+
(this.getLeft() == otherDomino.getRight() && this.getRight() == otherDomino.getLeft());
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
return Objects.hash(left, right);
29+
}
30+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
class Dominoes {
6+
7+
List<Domino> formChain(List<Domino> inputDominoes) throws ChainNotFoundException {
8+
if (inputDominoes.size() == 0) {
9+
return inputDominoes;
10+
} else {
11+
return formPartialChain(new ArrayList<Domino>(), inputDominoes);
12+
}
13+
}
14+
15+
private boolean isValidChain(ArrayList<Domino> chain) {
16+
return isValidPartialChain(chain) && (chain.get(0).getLeft() == chain.get(chain.size() - 1).getRight());
17+
}
18+
19+
private boolean isValidPartialChain(ArrayList<Domino> chain) {
20+
for (int i = 0; i < chain.size() - 1; i++) {
21+
if (chain.get(i).getRight() != chain.get(i+1).getLeft()) {
22+
return false;
23+
}
24+
}
25+
26+
return true;
27+
}
28+
29+
ArrayList<Domino> formPartialChain(ArrayList<Domino> current, List<Domino> remaining) throws ChainNotFoundException {
30+
if (remaining.size() == 0) {
31+
if (isValidChain(current)) {
32+
return current;
33+
} else {
34+
throw new ChainNotFoundException("No domino chain found.");
35+
}
36+
}
37+
38+
for (int i = 0; i < remaining.size(); i++) {
39+
ArrayList<Domino> newRemaining = new ArrayList<Domino>(remaining);
40+
newRemaining.remove(i);
41+
42+
ArrayList<Domino> newChainA = new ArrayList<Domino>(current);
43+
newChainA.add(remaining.get(i));
44+
45+
ArrayList<Domino> newChainB = new ArrayList<Domino>(current);
46+
newChainB.add(new Domino(remaining.get(i).getRight(), remaining.get(i).getLeft()));
47+
48+
if (isValidPartialChain(newChainA)) {
49+
try {
50+
return formPartialChain(newChainA, newRemaining);
51+
} catch (ChainNotFoundException e) {
52+
//This path does not have a valid chain
53+
}
54+
}
55+
56+
if (isValidPartialChain(newChainB)) {
57+
try {
58+
return formPartialChain(newChainB, newRemaining);
59+
} catch (ChainNotFoundException e) {
60+
//This path doesn't have a valid chain
61+
}
62+
}
63+
}
64+
65+
throw new ChainNotFoundException("No domino chain found.");
66+
}
67+
}

‎exercises/dominoes/.meta/version‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1.0.0
2+

‎exercises/dominoes/README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Dominoes
2+
3+
Make a chain of dominoes.
4+
5+
Compute a way to order a given set of dominoes in such a way that they form a
6+
correct domino chain (the dots on one half of a stone match the dots on the
7+
neighbouring half of an adjacent stone) and that dots on the halfs of the stones
8+
which don't have a neighbour (the first and last stone) match each other.
9+
10+
For example given the stones `[2|1]`, `[2|3]` and `[1|3]` you should compute something
11+
like `[1|2] [2|3] [3|1]` or `[3|2] [2|1] [1|3]` or `[1|3] [3|2] [2|1]` etc, where the first and last numbers are the same.
12+
13+
For stones `[1|2]`, `[4|1]` and `[2|3]` the resulting chain is not valid: `[4|1] [1|2] [2|3]`'s first and last numbers are not the same. 4 != 3
14+
15+
Some test cases may use duplicate stones in a chain solution, assume that multiple Domino sets are being used.
16+
17+
# Running the tests
18+
19+
You can run all the tests for an exercise by entering
20+
21+
```sh
22+
$ gradle test
23+
```
24+
25+
in your terminal.
26+
27+
## Submitting Incomplete Solutions
28+
29+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

‎exercises/dominoes/build.gradle‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ChainNotFoundException extends Exception {
2+
public ChainNotFoundException(String message) {
3+
super(message);
4+
}
5+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Objects;
2+
3+
class Domino {
4+
private int left;
5+
private int right;
6+
Domino(int left, int right) {
7+
this.left = left;
8+
this.right = right;
9+
}
10+
11+
int getLeft() {
12+
return this.left;
13+
}
14+
15+
int getRight() {
16+
return this.right;
17+
}
18+
19+
@Override
20+
public boolean equals(Object o) {
21+
Domino otherDomino = (Domino) o;
22+
return (this.getLeft() == otherDomino.getLeft() && this.getRight() == otherDomino.getRight()) ||
23+
(this.getLeft() == otherDomino.getRight() && this.getRight() == otherDomino.getLeft());
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
return Objects.hash(left, right);
29+
}
30+
}

0 commit comments

Comments
 (0)