Skip to content

Add tests, remove main, fix bug in Sparsity #5780

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 8 commits into from
Oct 15, 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 @@ -1033,6 +1033,7 @@
* [PalindromePrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java)
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
* [SparsityTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/SparsityTest.java)
* [ThreeSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java)
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
* [WordBoggleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/WordBoggleTest.java)
Expand Down
28 changes: 6 additions & 22 deletions src/main/java/com/thealgorithms/misc/Sparsity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.thealgorithms.misc;

import java.util.Scanner;

/*
*A matrix is sparse if many of its coefficients are zero (In general if 2/3rd of matrix elements
*are 0, it is considered as sparse). The interest in sparsity arises because its exploitation can
Expand All @@ -16,12 +14,17 @@ private Sparsity() {
}

/*
* @param mat the input matrix
* @return Sparsity of matrix
*
* where sparsity = number of zeroes/total elements in matrix
*
*/
static double sparsity(double[][] mat) {
if (mat == null || mat.length == 0) {
throw new IllegalArgumentException("Matrix cannot be null or empty");
}

int zero = 0;
// Traversing the matrix to count number of zeroes
for (int i = 0; i < mat.length; i++) {
Expand All @@ -32,25 +35,6 @@ static double sparsity(double[][] mat) {
}
}
// return sparsity
return ((double) zero / (mat.length * mat[1].length));
}

// Driver method
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter number of rows in matrix: ");
int n = in.nextInt();
System.out.println("Enter number of Columns in matrix: ");
int m = in.nextInt();

System.out.println("Enter Matrix elements: ");
double[][] mat = new double[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mat[i][j] = in.nextDouble();
}
}
System.out.println("Sparsity of matrix is: " + sparsity(mat));
in.close();
return ((double) zero / (mat.length * mat[0].length));
}
}
36 changes: 36 additions & 0 deletions src/test/java/com/thealgorithms/misc/SparsityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.thealgorithms.misc;

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

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class SparsityTest {

private static final double DELTA = 1e-9;

@ParameterizedTest(name = "Test case {index}: {2}")
@MethodSource("provideTestCases")
public void testSparsity(double[][] matrix, double expectedSparsity, String description) {
assertEquals(expectedSparsity, Sparsity.sparsity(matrix), DELTA, description);
}

private static Stream<Arguments> provideTestCases() {
return Stream.of(Arguments.of(new double[][] {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, 1.0, "Matrix with all zero elements"), Arguments.of(new double[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 0.0, "Matrix with no zero elements"),
Arguments.of(new double[][] {{0, 2, 0}, {4, 0, 6}, {0, 8, 0}}, 5.0 / 9.0, "Matrix with mixed elements"), Arguments.of(new double[][] {{0, 1, 0, 2, 0}}, 3.0 / 5.0, "Single-row matrix"), Arguments.of(new double[][] {{1}, {0}, {0}, {2}}, 2.0 / 4.0, "Single-column matrix"),
Arguments.of(new double[][] {{0}}, 1.0, "Matrix with a single zero element"), Arguments.of(new double[][] {{5}}, 0.0, "Matrix with a single non-zero element"));
}

@ParameterizedTest(name = "Test case {index}: {1}")
@MethodSource("provideExceptionTestCases")
public void testSparsityExceptions(double[][] matrix, String description) {
assertThrows(IllegalArgumentException.class, () -> Sparsity.sparsity(matrix), description);
}

private static Stream<Arguments> provideExceptionTestCases() {
return Stream.of(Arguments.of(new double[][] {}, "Empty matrix should throw IllegalArgumentException"));
}
}