Skip to content

Commit aa98d11

Browse files
committed
Adjust Factorial and couple of javadocs
1 parent 8323191 commit aa98d11

File tree

4 files changed

+114
-16
lines changed

4 files changed

+114
-16
lines changed

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

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,76 @@
55

66
public class Factorial {
77

8-
private static Map<Integer, Integer> cache = new HashMap<>();
8+
private final static Map<Integer, Integer> cache = new HashMap<>() {{
9+
put(0, 1);
10+
}};
911

10-
static {
11-
cache.put(0, 1);
12+
/**
13+
* Down-top approach
14+
*/
15+
public static int loop(int n) {
16+
if (n < 0) {
17+
throw new IllegalArgumentException("n should be >= 0");
18+
}
19+
20+
if (n == 0) {
21+
return 1;
22+
}
23+
24+
var result = 1;
25+
for (var i = 1; i <= n; i++) {
26+
result *= i;
27+
}
28+
return result;
29+
}
30+
31+
/**
32+
* Top-down approach
33+
*/
34+
public static int recursion(int n) {
35+
if (n < 0) {
36+
throw new IllegalArgumentException("n should be >= 0");
37+
}
38+
39+
if (n == 0) {
40+
return 1;
41+
}
42+
43+
return n * recursion(n - 1);
1244
}
1345

14-
public static int calculate(int n) {
15-
if (n < 0) throw new IllegalArgumentException("Wrong parameter");
46+
/**
47+
* Top-down approach
48+
*/
49+
public static int recursionWithCache(int n) {
50+
if (n < 0) {
51+
throw new IllegalArgumentException("n should be >= 0");
52+
}
1653

1754
if (cache.containsKey(n)) {
1855
return cache.get(n);
1956
}
2057

21-
int result = n * calculate(n - 1);
58+
int result = n * recursionWithCache(n - 1);
2259
cache.put(n, result);
2360
return result;
2461
}
62+
63+
/**
64+
* Down-top approach
65+
*/
66+
public static int loopWithCache(int n) {
67+
if (n < 0) {
68+
throw new IllegalArgumentException("n should be >= 0");
69+
}
70+
71+
if (cache.containsKey(n)) {
72+
return cache.get(n);
73+
}
74+
75+
for (var i = 1; i <= n; i++) {
76+
cache.put(i, i * cache.get(i - 1));
77+
}
78+
return cache.get(n);
79+
}
2580
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static int calculate(int n) {
2828
}
2929

3030
/**
31-
* Down-top approach (just as example)
31+
* Down-top approach (loop, just as example)
3232
*/
3333
public static int calculate2(int n) {
3434
if (n < 0) throw new IllegalArgumentException("Number should be not less than 0!");

src/main/java/by/andd3dfx/string/StringUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static String revert(String string) {
1515
/**
1616
* Using the Java language, have the function LetterChanges(str) take the str parameter being passed and modify it
1717
* using the following algorithm. Replace every letter in the string with the letter following it in the alphabet
18-
* (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return
18+
* (i.e. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return
1919
* this modified string.
2020
*/
2121
public static String shiftCharactersAndCapitalizeVowels(String str) {

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

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,60 @@
77

88
public class FactorialTest {
99

10+
@Test
11+
public void loop() {
12+
assertThat("0!=1", Factorial.loop(0), is(1));
13+
assertThat("1!=1", Factorial.loop(1), is(1));
14+
assertThat("2!=2", Factorial.loop(2), is(2));
15+
assertThat("3!=6", Factorial.loop(3), is(6));
16+
assertThat("8!=40320", Factorial.loop(8), is(40320));
17+
}
18+
19+
20+
@Test(expected = IllegalArgumentException.class)
21+
public void loopForNegativeParam() {
22+
Factorial.loop(-1);
23+
}
24+
25+
@Test
26+
public void recursion() {
27+
assertThat("0!=1", Factorial.recursion(0), is(1));
28+
assertThat("1!=1", Factorial.recursion(1), is(1));
29+
assertThat("2!=2", Factorial.recursion(2), is(2));
30+
assertThat("3!=6", Factorial.recursion(3), is(6));
31+
assertThat("8!=40320", Factorial.recursion(8), is(40320));
32+
}
33+
1034
@Test(expected = IllegalArgumentException.class)
11-
public void calculateForNegativeParam() {
12-
Factorial.calculate(-1);
35+
public void recursionForNegativeParam() {
36+
Factorial.recursion(-1);
1337
}
1438

1539
@Test
16-
public void calculate() {
17-
assertThat("0!=1", Factorial.calculate(0), is(1));
18-
assertThat("1!=1", Factorial.calculate(1), is(1));
19-
assertThat("2!=2", Factorial.calculate(2), is(2));
20-
assertThat("3!=6", Factorial.calculate(3), is(6));
21-
assertThat("8!=40320", Factorial.calculate(8), is(40320));
40+
public void recursionWithCache() {
41+
assertThat("0!=1", Factorial.recursionWithCache(0), is(1));
42+
assertThat("1!=1", Factorial.recursionWithCache(1), is(1));
43+
assertThat("2!=2", Factorial.recursionWithCache(2), is(2));
44+
assertThat("3!=6", Factorial.recursionWithCache(3), is(6));
45+
assertThat("8!=40320", Factorial.recursionWithCache(8), is(40320));
46+
}
47+
48+
@Test(expected = IllegalArgumentException.class)
49+
public void recursionWithCacheForNegativeParam() {
50+
Factorial.recursionWithCache(-1);
51+
}
52+
53+
@Test
54+
public void loopWithCache() {
55+
assertThat("0!=1", Factorial.loopWithCache(0), is(1));
56+
assertThat("1!=1", Factorial.loopWithCache(1), is(1));
57+
assertThat("2!=2", Factorial.loopWithCache(2), is(2));
58+
assertThat("3!=6", Factorial.loopWithCache(3), is(6));
59+
assertThat("8!=40320", Factorial.loopWithCache(8), is(40320));
60+
}
61+
62+
@Test(expected = IllegalArgumentException.class)
63+
public void loopWithCacheForNegativeParam() {
64+
Factorial.loopWithCache(-1);
2265
}
2366
}

0 commit comments

Comments
 (0)