Skip to content

Commit 6465ab9

Browse files
committed
Add top-down / down-top approaches examples (for Fibonacci task)
1 parent 48b8aa9 commit 6465ab9

File tree

2 files changed

+65
-23
lines changed

2 files changed

+65
-23
lines changed

src/main/java/by/andd3dfx/numeric/Fibonacci.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,39 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
/**
7+
* Check this article for comparison of top-down / down-top approaches:
8+
* https://habr.com/ru/post/423939/
9+
*/
610
public class Fibonacci {
711

8-
private static final Map<Integer, Long> map = new HashMap<Integer, Long>() {{
12+
private static final Map<Integer, Long> map = new HashMap<>() {{
13+
put(0, 0L);
914
put(1, 1L);
10-
put(2, 1L);
1115
}};
1216

13-
public static long get(int n) {
14-
if (n < 1) throw new IllegalArgumentException("Number should be greater than 0!");
17+
/**
18+
* Top-down approach (recursive, better)
19+
*/
20+
public static long calculate(int n) {
21+
if (n < 0) throw new IllegalArgumentException("Number should be not less than 0!");
1522

1623
if (!map.containsKey(n)) {
17-
map.put(n, get(n - 1) + get(n - 2));
24+
map.put(n, calculate(n - 1) + calculate(n - 2));
1825
}
1926

2027
return map.get(n);
2128
}
29+
30+
/**
31+
* Down-top approach (just as example)
32+
*/
33+
public static long calculate2(int n) {
34+
if (n < 0) throw new IllegalArgumentException("Number should be not less than 0!");
35+
36+
for (int i = 2; i <= n; i++) {
37+
map.put(n, map.get(n - 1) + map.get(n - 2));
38+
}
39+
return map.get(n);
40+
}
2241
}

src/test/java/by/andd3dfx/numeric/FibonacciTest.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,58 @@
66

77
import org.junit.Test;
88

9+
import java.util.function.Function;
10+
911
public class FibonacciTest {
1012

1113
@Test
12-
public void get() {
13-
assertThat("Expected F(2) = 1", Fibonacci.get(2), is(1L));
14-
assertThat("Expected F(4) = 3", Fibonacci.get(4), is(3L));
15-
assertThat("Expected F(1) = 1", Fibonacci.get(1), is(1L));
16-
assertThat("Expected F(8) = 21", Fibonacci.get(8), is(21L));
17-
assertThat("Expected F(9) = 34", Fibonacci.get(9), is(34L));
18-
assertThat("Expected F(3) = 2", Fibonacci.get(3), is(2L));
19-
assertThat("Expected F(7) = 13", Fibonacci.get(7), is(13L));
20-
assertThat("Expected F(10) = 55", Fibonacci.get(10), is(55L));
21-
assertThat("Expected F(6) = 8", Fibonacci.get(6), is(8L));
22-
assertThat("Expected F(5) = 5", Fibonacci.get(5), is(5L));
14+
public void calculate() {
15+
assertThat("Expected F(2) = 1", Fibonacci.calculate(2), is(1L));
16+
assertThat("Expected F(4) = 3", Fibonacci.calculate(4), is(3L));
17+
assertThat("Expected F(1) = 1", Fibonacci.calculate(1), is(1L));
18+
assertThat("Expected F(0) = 0", Fibonacci.calculate(0), is(0L));
19+
assertThat("Expected F(8) = 21", Fibonacci.calculate(8), is(21L));
20+
assertThat("Expected F(9) = 34", Fibonacci.calculate(9), is(34L));
21+
assertThat("Expected F(3) = 2", Fibonacci.calculate(3), is(2L));
22+
assertThat("Expected F(7) = 13", Fibonacci.calculate(7), is(13L));
23+
assertThat("Expected F(10) = 55", Fibonacci.calculate(10), is(55L));
24+
assertThat("Expected F(6) = 8", Fibonacci.calculate(6), is(8L));
25+
assertThat("Expected F(5) = 5", Fibonacci.calculate(5), is(5L));
26+
}
27+
28+
@Test
29+
public void calculate2() {
30+
assertThat("Expected F(2) = 1", Fibonacci.calculate2(2), is(1L));
31+
assertThat("Expected F(4) = 3", Fibonacci.calculate2(4), is(3L));
32+
assertThat("Expected F(1) = 1", Fibonacci.calculate2(1), is(1L));
33+
assertThat("Expected F(0) = 0", Fibonacci.calculate2(0), is(0L));
34+
assertThat("Expected F(8) = 21", Fibonacci.calculate2(8), is(21L));
35+
assertThat("Expected F(9) = 34", Fibonacci.calculate2(9), is(34L));
36+
assertThat("Expected F(3) = 2", Fibonacci.calculate2(3), is(2L));
37+
assertThat("Expected F(7) = 13", Fibonacci.calculate2(7), is(13L));
38+
assertThat("Expected F(10) = 55", Fibonacci.calculate2(10), is(55L));
39+
assertThat("Expected F(6) = 8", Fibonacci.calculate2(6), is(8L));
40+
assertThat("Expected F(5) = 5", Fibonacci.calculate2(5), is(5L));
41+
}
42+
43+
@Test
44+
public void calculateForWrongParam() {
45+
checkIsExceptionThrown(Fibonacci::calculate, -1);
46+
checkIsExceptionThrown(Fibonacci::calculate, -10);
2347
}
2448

2549
@Test
26-
public void getForWrongParam() {
27-
checkIsExceptionThrown(0);
28-
checkIsExceptionThrown(-1);
29-
checkIsExceptionThrown(-10);
50+
public void calculate2ForWrongParam() {
51+
checkIsExceptionThrown(Fibonacci::calculate2, -1);
52+
checkIsExceptionThrown(Fibonacci::calculate2, -10);
3053
}
3154

32-
private void checkIsExceptionThrown(int n) {
55+
private void checkIsExceptionThrown(Function<Integer, Long> function, int n) {
3356
try {
34-
Fibonacci.get(n);
57+
function.apply(n);
3558
fail("Exception should be thrown");
3659
} catch (IllegalArgumentException iae) {
37-
assertThat("Wrong message", iae.getMessage(), is("Number should be greater than 0!"));
60+
assertThat("Wrong message", iae.getMessage(), is("Number should be not less than 0!"));
3861
}
3962
}
4063
}

0 commit comments

Comments
 (0)