Skip to content

Commit 40e101d

Browse files
KineolyanFridaTveit
authored andcommitted
Adding the grain exercise. (exercism#1755)
* Start adding the exercise. * Add entry in the gradle project for the exercise. * Update the README. * Implement the tests and the basic structure of the class for this exercise. * Set the correct version. * Pass checkstyle. * Provide reference implementation. * Correct code. * Correct reference implementation. * Apply PR suggestions. * Fix order of annotations and remove comments. * Review the level of the exercise. * Correct the position of the exercise in the config. * Correct assertion param orders and do not use static methods. * Update error assertions to ExpectedException.
1 parent 6fe74b5 commit 40e101d

8 files changed

Lines changed: 230 additions & 0 deletions

File tree

config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@
9494
"randomness"
9595
]
9696
},
97+
{
98+
"slug": "grains",
99+
"uuid": "5ee66f39-5e37-4907-a6d9-f55d38324c6c",
100+
"core": false,
101+
"unlocked_by": "two-fer",
102+
"difficulty": 2,
103+
"topics": [
104+
"loops",
105+
"math"
106+
],
107+
"deprecated": false
108+
},
97109
{
98110
"slug": "pangram",
99111
"uuid": "133b0f84-bdc7-4508-a0a1-5732a7db81ef",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.math.BigInteger;
2+
import java.util.stream.IntStream;
3+
4+
class Grains {
5+
6+
BigInteger computeNumberOfGrainsOnSquare(final int square) {
7+
if (1 <= square && square <= 64) {
8+
return BigInteger.valueOf(2).pow(square - 1);
9+
} else {
10+
throw new IllegalArgumentException("square must be between 1 and 64");
11+
}
12+
}
13+
14+
BigInteger computeTotalNumberOfGrainsOnBoard() {
15+
return IntStream.rangeClosed(1, 64)
16+
.mapToObj(this::computeNumberOfGrainsOnSquare)
17+
.reduce(
18+
BigInteger.valueOf(0),
19+
BigInteger::add);
20+
}
21+
22+
}

exercises/grains/.meta/version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.2.0

exercises/grains/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Grains
2+
3+
Calculate the number of grains of wheat on a chessboard given that the number
4+
on each square doubles.
5+
6+
There once was a wise servant who saved the life of a prince. The king
7+
promised to pay whatever the servant could dream up. Knowing that the
8+
king loved chess, the servant told the king he would like to have grains
9+
of wheat. One grain on the first square of a chess board, with the number
10+
of grains doubling on each successive square.
11+
12+
There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).
13+
14+
Write code that shows:
15+
- how many grains were on a given square, and
16+
- the total number of grains on the chessboard
17+
18+
## For bonus points
19+
20+
Did you get the tests passing and the code clean? If you want to, these
21+
are some additional things you could try:
22+
23+
- Optimize for speed.
24+
- Optimize for readability.
25+
26+
Then please share your thoughts in a comment on the submission. Did this
27+
experiment make the code better? Worse? Did you learn anything from it?
28+
29+
## Setup
30+
31+
Go through the setup instructions for Java to install the necessary
32+
dependencies:
33+
34+
[https://exercism.io/tracks/java/installation](https://exercism.io/tracks/java/installation)
35+
36+
# Running the tests
37+
38+
You can run all the tests for an exercise by entering the following in your
39+
terminal:
40+
41+
```sh
42+
$ gradle test
43+
```
44+
45+
> Use `gradlew.bat` if you're on Windows
46+
47+
In the test suites all tests but the first have been skipped.
48+
49+
Once you get a test passing, you can enable the next one by removing the
50+
`@Ignore("Remove to run test")` annotation.
51+
52+
## Source
53+
54+
JavaRanch Cattle Drive, exercise 6 [http://www.javaranch.com/grains.jsp](http://www.javaranch.com/grains.jsp)
55+
56+
## Submitting Incomplete Solutions
57+
It's possible to submit an incomplete solution so you can see how others have
58+
completed the exercise.

exercises/grains/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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.math.BigInteger;
2+
3+
class Grains {
4+
5+
BigInteger computeNumberOfGrainsOnSquare(final int square) {
6+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
7+
}
8+
9+
BigInteger computeTotalNumberOfGrainsOnBoard() {
10+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
11+
}
12+
13+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import org.junit.Before;
2+
import org.junit.Ignore;
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
import org.junit.rules.ExpectedException;
6+
7+
import java.math.BigInteger;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class GrainsTest {
12+
13+
private static String wrongSquareMessage = "square must be between 1 and 64";
14+
15+
@Rule
16+
public ExpectedException expectedException = ExpectedException.none();
17+
18+
private Grains grains;
19+
20+
@Before
21+
public void setup() {
22+
grains = new Grains();
23+
}
24+
25+
26+
@Test
27+
public void countAtSquare1() {
28+
BigInteger result = grains.computeNumberOfGrainsOnSquare(1);
29+
assertEquals(new BigInteger("1"), result);
30+
}
31+
32+
@Ignore("Remove to run test")
33+
@Test
34+
public void countAtSquare2() {
35+
BigInteger result = grains.computeNumberOfGrainsOnSquare(2);
36+
assertEquals(new BigInteger("2"), result);
37+
}
38+
39+
@Ignore("Remove to run test")
40+
@Test
41+
public void countAtSquare3() {
42+
BigInteger result = grains.computeNumberOfGrainsOnSquare(3);
43+
assertEquals(new BigInteger("4"), result);
44+
}
45+
46+
@Ignore("Remove to run test")
47+
@Test
48+
public void countAtSquare4() {
49+
BigInteger result = grains.computeNumberOfGrainsOnSquare(4);
50+
assertEquals(new BigInteger("8"), result);
51+
}
52+
53+
@Ignore("Remove to run test")
54+
@Test
55+
public void countAtSquare16() {
56+
BigInteger result = grains.computeNumberOfGrainsOnSquare(16);
57+
assertEquals(new BigInteger("32768"), result);
58+
}
59+
60+
@Ignore("Remove to run test")
61+
@Test
62+
public void countAtSquare32() {
63+
BigInteger result = grains.computeNumberOfGrainsOnSquare(32);
64+
assertEquals(new BigInteger("2147483648"), result);
65+
}
66+
67+
@Ignore("Remove to run test")
68+
@Test
69+
public void countAtSquare64() {
70+
BigInteger result = grains.computeNumberOfGrainsOnSquare(64);
71+
assertEquals(new BigInteger("9223372036854775808"), result);
72+
}
73+
74+
@Ignore("Remove to run test")
75+
@Test
76+
public void errorOnNullBoardSize() {
77+
expectedException.expect(IllegalArgumentException.class);
78+
expectedException.expectMessage(wrongSquareMessage);
79+
grains.computeNumberOfGrainsOnSquare(0);
80+
}
81+
82+
@Ignore("Remove to run test")
83+
@Test
84+
public void errorOnNegativeBoardSize() {
85+
expectedException.expect(IllegalArgumentException.class);
86+
expectedException.expectMessage(wrongSquareMessage);
87+
grains.computeNumberOfGrainsOnSquare(-1);
88+
}
89+
90+
@Ignore("Remove to run test")
91+
@Test
92+
public void errorOnExcessiveBoardSize() {
93+
expectedException.expect(IllegalArgumentException.class);
94+
expectedException.expectMessage(wrongSquareMessage);
95+
grains.computeNumberOfGrainsOnSquare(65);
96+
}
97+
98+
@Ignore("Remove to run test")
99+
@Test
100+
public void totalNumberOfGrainsOnABoard() {
101+
BigInteger total = grains.computeTotalNumberOfGrainsOnBoard();
102+
assertEquals(new BigInteger("18446744073709551615"), total);
103+
}
104+
105+
}

exercises/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ include 'forth'
3636
include 'gigasecond'
3737
include 'go-counting'
3838
include 'grade-school'
39+
include 'grains'
3940
include 'grep'
4041
include 'hamming'
4142
include 'hexadecimal'

0 commit comments

Comments
 (0)