Skip to content

Commit f66da5e

Browse files
authored
refactor: Enhance docs, add tests in PrintMatrixInSpiralOrder (#6636)
* refactor: Enhance docs, add tests in `PrintMatrixInSpiralOrder` * Fix error in BloomFilter * Fix * Fix * Fix
1 parent 8930369 commit f66da5e

File tree

6 files changed

+135
-121
lines changed

6 files changed

+135
-121
lines changed

src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
/**
77
* A generic BloomFilter implementation for probabilistic membership checking.
88
* <p>
9-
* Bloom filters are space-efficient data structures that provide a fast way to test whether an
10-
* element is a member of a set. They may produce false positives, indicating an element is
9+
* Bloom filters are space-efficient data structures that provide a fast way to
10+
* test whether an
11+
* element is a member of a set. They may produce false positives, indicating an
12+
* element is
1113
* in the set when it is not, but they will never produce false negatives.
1214
* </p>
1315
*
@@ -21,11 +23,14 @@ public class BloomFilter<T> {
2123
private final Hash<T>[] hashFunctions;
2224

2325
/**
24-
* Constructs a BloomFilter with a specified number of hash functions and bit array size.
26+
* Constructs a BloomFilter with a specified number of hash functions and bit
27+
* array size.
2528
*
2629
* @param numberOfHashFunctions the number of hash functions to use
27-
* @param bitArraySize the size of the bit array, which determines the capacity of the filter
28-
* @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is less than 1
30+
* @param bitArraySize the size of the bit array, which determines the
31+
* capacity of the filter
32+
* @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is
33+
* less than 1
2934
*/
3035
@SuppressWarnings("unchecked")
3136
public BloomFilter(int numberOfHashFunctions, int bitArraySize) {
@@ -39,7 +44,8 @@ public BloomFilter(int numberOfHashFunctions, int bitArraySize) {
3944
}
4045

4146
/**
42-
* Initializes the hash functions with unique indices to ensure different hashing.
47+
* Initializes the hash functions with unique indices to ensure different
48+
* hashing.
4349
*/
4450
private void initializeHashFunctions() {
4551
for (int i = 0; i < numberOfHashFunctions; i++) {
@@ -50,7 +56,8 @@ private void initializeHashFunctions() {
5056
/**
5157
* Inserts an element into the Bloom filter.
5258
* <p>
53-
* This method hashes the element using all defined hash functions and sets the corresponding
59+
* This method hashes the element using all defined hash functions and sets the
60+
* corresponding
5461
* bits in the bit array.
5562
* </p>
5663
*
@@ -66,13 +73,16 @@ public void insert(T key) {
6673
/**
6774
* Checks if an element might be in the Bloom filter.
6875
* <p>
69-
* This method checks the bits at the positions computed by each hash function. If any of these
70-
* bits are not set, the element is definitely not in the filter. If all bits are set, the element
76+
* This method checks the bits at the positions computed by each hash function.
77+
* If any of these
78+
* bits are not set, the element is definitely not in the filter. If all bits
79+
* are set, the element
7180
* might be in the filter.
7281
* </p>
7382
*
7483
* @param key the element to check for membership in the Bloom filter
75-
* @return {@code true} if the element might be in the Bloom filter, {@code false} if it is definitely not
84+
* @return {@code true} if the element might be in the Bloom filter,
85+
* {@code false} if it is definitely not
7686
*/
7787
public boolean contains(T key) {
7888
for (Hash<T> hash : hashFunctions) {
@@ -87,7 +97,8 @@ public boolean contains(T key) {
8797
/**
8898
* Inner class representing a hash function used by the Bloom filter.
8999
* <p>
90-
* Each instance of this class represents a different hash function based on its index.
100+
* Each instance of this class represents a different hash function based on its
101+
* index.
91102
* </p>
92103
*
93104
* @param <T> The type of elements to be hashed.

src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,41 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
/**
7+
* Utility class to print a matrix in spiral order.
8+
* <p>
9+
* Given a 2D array (matrix), this class provides a method to return the
10+
* elements
11+
* of the matrix in spiral order, starting from the top-left corner and moving
12+
* clockwise.
13+
* </p>
14+
*
15+
* @author Sadiul Hakim (https://github.com/sadiul-hakim)
16+
*/
617
public class PrintAMatrixInSpiralOrder {
18+
719
/**
8-
* Search a key in row and column wise sorted matrix
20+
* Returns the elements of the given matrix in spiral order.
21+
*
22+
* @param matrix the 2D array to traverse in spiral order
23+
* @param row the number of rows in the matrix
24+
* @param col the number of columns in the matrix
25+
* @return a list containing the elements of the matrix in spiral order
926
*
10-
* @param matrix matrix to be searched
11-
* @param row number of rows matrix has
12-
* @param col number of columns matrix has
13-
* @author Sadiul Hakim : https://github.com/sadiul-hakim
27+
* <p>
28+
* Example:
29+
*
30+
* <pre>
31+
* int[][] matrix = {
32+
* {1, 2, 3},
33+
* {4, 5, 6},
34+
* {7, 8, 9}
35+
* };
36+
* print(matrix, 3, 3) returns [1, 2, 3, 6, 9, 8, 7, 4, 5]
37+
* </pre>
38+
* </p>
1439
*/
1540
public List<Integer> print(int[][] matrix, int row, int col) {
16-
1741
// r traverses matrix row wise from first
1842
int r = 0;
1943
// c traverses matrix column wise from first

src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.thealgorithms.matrix;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import org.junit.jupiter.api.Test;
9+
10+
class PrintAMatrixInSpiralOrderTest {
11+
12+
private final PrintAMatrixInSpiralOrder spiralPrinter = new PrintAMatrixInSpiralOrder();
13+
14+
@Test
15+
void testSquareMatrix() {
16+
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
17+
List<Integer> expected = Arrays.asList(1, 2, 3, 6, 9, 8, 7, 4, 5);
18+
assertEquals(expected, spiralPrinter.print(matrix, 3, 3));
19+
}
20+
21+
@Test
22+
void testRectangularMatrixMoreRows() {
23+
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
24+
List<Integer> expected = Arrays.asList(1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8);
25+
assertEquals(expected, spiralPrinter.print(matrix, 4, 3));
26+
}
27+
28+
@Test
29+
void testRectangularMatrixMoreCols() {
30+
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
31+
List<Integer> expected = Arrays.asList(1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7);
32+
assertEquals(expected, spiralPrinter.print(matrix, 3, 4));
33+
}
34+
35+
@Test
36+
void testSingleRow() {
37+
int[][] matrix = {{1, 2, 3, 4}};
38+
List<Integer> expected = Arrays.asList(1, 2, 3, 4);
39+
assertEquals(expected, spiralPrinter.print(matrix, 1, 4));
40+
}
41+
42+
@Test
43+
void testSingleColumn() {
44+
int[][] matrix = {{1}, {2}, {3}};
45+
List<Integer> expected = Arrays.asList(1, 2, 3);
46+
assertEquals(expected, spiralPrinter.print(matrix, 3, 1));
47+
}
48+
49+
@Test
50+
void testEmptyMatrix() {
51+
int[][] matrix = new int[0][0];
52+
List<Integer> expected = Collections.emptyList();
53+
assertEquals(expected, spiralPrinter.print(matrix, 0, 0));
54+
}
55+
56+
@Test
57+
void testOneElementMatrix() {
58+
int[][] matrix = {{42}};
59+
List<Integer> expected = Collections.singletonList(42);
60+
assertEquals(expected, spiralPrinter.print(matrix, 1, 1));
61+
}
62+
63+
@Test
64+
void testMatrixWithNegativeNumbers() {
65+
int[][] matrix = {{-1, -2}, {-3, -4}};
66+
List<Integer> expected = Arrays.asList(-1, -2, -4, -3);
67+
assertEquals(expected, spiralPrinter.print(matrix, 2, 2));
68+
}
69+
70+
@Test
71+
void testLargeSquareMatrix() {
72+
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
73+
List<Integer> expected = Arrays.asList(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25, 24, 15, 16);
74+
assertEquals(expected, spiralPrinter.print(matrix, 5, 5));
75+
}
76+
77+
@Test
78+
void testSingleRowWithTwoElements() {
79+
int[][] matrix = {{2, 2}};
80+
List<Integer> expected = Arrays.asList(2, 2);
81+
assertEquals(expected, spiralPrinter.print(matrix, 1, 2));
82+
}
83+
}

src/test/java/com/thealgorithms/matrix/TestPrintMatrixInSpiralOrder.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)