Skip to content

Commit fe972d0

Browse files
committed
collatz-conjecture: add to track
1 parent 247371e commit fe972d0

6 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@
9595
"difficulty": 4,
9696
"topics": []
9797
},
98+
{
99+
"slug": "collatz-conjecture",
100+
"difficulty": 4,
101+
"topics": []
102+
},
98103
{
99104
"slug": "nth-prime",
100105
"difficulty": 4,
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class CollatzCalculator {
2+
3+
int computeStepCount(final int start) {
4+
if (start <= 0) throw new IllegalArgumentException("Only natural numbers are allowed");
5+
6+
if (start == 1) return 0;
7+
8+
final int next;
9+
10+
if (start % 2 == 0) {
11+
next = start / 2;
12+
} else{
13+
next = 3 * start + 1;
14+
}
15+
16+
return 1 + computeStepCount(next);
17+
}
18+
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class CollatzCalculator {
2+
3+
int computeStepCount(int start) {
4+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5+
}
6+
7+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 static org.junit.Assert.assertEquals;
8+
9+
/*
10+
* version: 1.0.0
11+
*/
12+
public class CollatzCalculatorTest {
13+
14+
@Rule
15+
public ExpectedException expectedException = ExpectedException.none();
16+
17+
private CollatzCalculator collatzCalculator;
18+
19+
@Before
20+
public void setUp() {
21+
collatzCalculator = new CollatzCalculator();
22+
}
23+
24+
@Test
25+
public void testZeroStepsRequiredWhenStartingFrom1() {
26+
assertEquals(0, collatzCalculator.computeStepCount(1));
27+
}
28+
29+
@Ignore("Remove to run test")
30+
@Test
31+
public void testCorrectNumberOfStepsWhenAllStepsAreDivisions() {
32+
assertEquals(4, collatzCalculator.computeStepCount(16));
33+
}
34+
35+
@Ignore("Remove to run test")
36+
@Test
37+
public void testCorrectNumberOfStepsWhenBothStepTypesAreNeeded() {
38+
assertEquals(9, collatzCalculator.computeStepCount(12));
39+
}
40+
41+
@Ignore("Remove to run test")
42+
@Test
43+
public void testZeroIsConsideredInvalidInput() {
44+
expectedException.expect(IllegalArgumentException.class);
45+
expectedException.expectMessage("Only natural numbers are allowed");
46+
47+
collatzCalculator.computeStepCount(0);
48+
}
49+
50+
@Ignore("Remove to run test")
51+
@Test
52+
public void testNegativeIntegerIsConsideredInvalidInput() {
53+
expectedException.expect(IllegalArgumentException.class);
54+
expectedException.expectMessage("Only natural numbers are allowed");
55+
56+
collatzCalculator.computeStepCount(-15);
57+
}
58+
59+
}

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ include 'book-store'
1313
include 'bracket-push'
1414
include 'change'
1515
include 'clock'
16+
include 'collatz-conjecture'
1617
include 'crypto-square'
1718
include 'custom-set'
1819
include 'diamond'

0 commit comments

Comments
 (0)