Skip to content

Commit c3148dd

Browse files
HardvanChiefpatwal
authored andcommitted
Add tests, remove main in DiceThrow/DP.java (TheAlgorithms#5644)
1 parent deaa7f9 commit c3148dd

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@
788788
* [ClimbStairsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/ClimbStairsTest.java)
789789
* [CoinChangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/CoinChangeTest.java)
790790
* [CountFriendsPairingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/CountFriendsPairingTest.java)
791+
* [DPTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/DPTest.java)
791792
* [EditDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EditDistanceTest.java)
792793
* [EggDroppingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java)
793794
* [KnapsackMemoizationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackMemoizationTest.java)

src/main/java/com/thealgorithms/dynamicprogramming/DiceThrow.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/* Hence, storing the results of the solved sub-problems saves time.
1111
And it can be done using Dynamic Programming(DP).
12+
// Time Complexity: O(m * n * x) where m is number of faces, n is number of dice and x is given sum.
1213
Following is implementation of Dynamic Programming approach. */
1314
// Code ---->
1415
// Java program to find number of ways to get sum 'x' with 'n'
@@ -43,21 +44,4 @@ public static long findWays(int m, int n, int x) {
4344

4445
return table[n][x];
4546
}
46-
47-
public static void main(String[] args) {
48-
System.out.println(findWays(4, 2, 1));
49-
System.out.println(findWays(2, 2, 3));
50-
System.out.println(findWays(6, 3, 8));
51-
System.out.println(findWays(4, 2, 5));
52-
System.out.println(findWays(4, 3, 5));
53-
}
5447
}
55-
/*
56-
OUTPUT:
57-
0
58-
2
59-
21
60-
4
61-
6
62-
*/
63-
// Time Complexity: O(m * n * x) where m is number of faces, n is number of dice and x is given sum.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class DPTest {
8+
9+
@Test
10+
void testSumLessThanMinimumFaceValue() {
11+
// When the sum is less than the minimum possible face value
12+
// There are 0 ways to achieve the sum
13+
assertEquals(0, DP.findWays(4, 2, 1)); // 4 faces, 2 dice, sum = 1
14+
}
15+
16+
@Test
17+
void testTwoDiceWithSumEqualToTwo() {
18+
// When there are 2 dice and the sum is equal to the number of dice
19+
// The only way is to have both dice showing 1
20+
assertEquals(1, DP.findWays(2, 2, 2)); // 2 faces, 2 dice, sum = 2
21+
}
22+
23+
@Test
24+
void testTwoDiceWithSumThree() {
25+
// When there are 2 dice and the sum is equal to 3
26+
// Possible combinations are (1,2) and (2,1)
27+
assertEquals(2, DP.findWays(2, 2, 3)); // 2 faces, 2 dice, sum = 3
28+
}
29+
30+
@Test
31+
void testThreeDiceWithSumEight() {
32+
// Test for 3 dice, each having 6 faces
33+
// Possible combinations to make sum of 8
34+
assertEquals(21, DP.findWays(6, 3, 8)); // 6 faces, 3 dice, sum = 8
35+
}
36+
37+
@Test
38+
void testTwoDiceWithSumFive() {
39+
// Test for 2 dice, with 4 faces to make sum of 5
40+
// Possible combinations: (1,4), (2,3), (3,2), (4,1)
41+
assertEquals(4, DP.findWays(4, 2, 5)); // 4 faces, 2 dice, sum = 5
42+
}
43+
44+
@Test
45+
void testThreeDiceWithSumFive() {
46+
// Test for 3 dice, with 4 faces to make sum of 5
47+
// Possible combinations: (1,1,3), (1,2,2), (1,3,1), (2,1,2), (2,2,1), (3,1,1)
48+
assertEquals(6, DP.findWays(4, 3, 5)); // 4 faces, 3 dice, sum = 5
49+
}
50+
51+
@Test
52+
void testEdgeCaseZeroSum() {
53+
// Test for 0 sum with 0 dice
54+
assertEquals(0, DP.findWays(4, 0, 0)); // 4 faces, 0 dice, sum = 0
55+
}
56+
}

0 commit comments

Comments
 (0)