Skip to content

Add tests, remove main in DiceThrow/DP.java #5644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@
* [ClimbStairsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/ClimbStairsTest.java)
* [CoinChangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/CoinChangeTest.java)
* [CountFriendsPairingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/CountFriendsPairingTest.java)
* [DPTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/DPTest.java)
* [EditDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EditDistanceTest.java)
* [EggDroppingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java)
* [KnapsackMemoizationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackMemoizationTest.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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

return table[n][x];
}

public static void main(String[] args) {
System.out.println(findWays(4, 2, 1));
System.out.println(findWays(2, 2, 3));
System.out.println(findWays(6, 3, 8));
System.out.println(findWays(4, 2, 5));
System.out.println(findWays(4, 3, 5));
}
}
/*
OUTPUT:
0
2
21
4
6
*/
// Time Complexity: O(m * n * x) where m is number of faces, n is number of dice and x is given sum.
56 changes: 56 additions & 0 deletions src/test/java/com/thealgorithms/dynamicprogramming/DPTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class DPTest {

@Test
void testSumLessThanMinimumFaceValue() {
// When the sum is less than the minimum possible face value
// There are 0 ways to achieve the sum
assertEquals(0, DP.findWays(4, 2, 1)); // 4 faces, 2 dice, sum = 1
}

@Test
void testTwoDiceWithSumEqualToTwo() {
// When there are 2 dice and the sum is equal to the number of dice
// The only way is to have both dice showing 1
assertEquals(1, DP.findWays(2, 2, 2)); // 2 faces, 2 dice, sum = 2
}

@Test
void testTwoDiceWithSumThree() {
// When there are 2 dice and the sum is equal to 3
// Possible combinations are (1,2) and (2,1)
assertEquals(2, DP.findWays(2, 2, 3)); // 2 faces, 2 dice, sum = 3
}

@Test
void testThreeDiceWithSumEight() {
// Test for 3 dice, each having 6 faces
// Possible combinations to make sum of 8
assertEquals(21, DP.findWays(6, 3, 8)); // 6 faces, 3 dice, sum = 8
}

@Test
void testTwoDiceWithSumFive() {
// Test for 2 dice, with 4 faces to make sum of 5
// Possible combinations: (1,4), (2,3), (3,2), (4,1)
assertEquals(4, DP.findWays(4, 2, 5)); // 4 faces, 2 dice, sum = 5
}

@Test
void testThreeDiceWithSumFive() {
// Test for 3 dice, with 4 faces to make sum of 5
// Possible combinations: (1,1,3), (1,2,2), (1,3,1), (2,1,2), (2,2,1), (3,1,1)
assertEquals(6, DP.findWays(4, 3, 5)); // 4 faces, 3 dice, sum = 5
}

@Test
void testEdgeCaseZeroSum() {
// Test for 0 sum with 0 dice
assertEquals(0, DP.findWays(4, 0, 0)); // 4 faces, 0 dice, sum = 0
}
}