From 32e2191e9b3cefafa88c171e797a880eb65c1717 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 5 Oct 2025 13:04:10 +0530 Subject: [PATCH 1/5] refactor: Enhance docs, add tests in `PrintMatrixInSpiralOrder` --- .../others/PrintAMatrixInSpiralOrder.java | 35 ++++++++-- .../others/PrintAMatrixInSpiralOrderTest.java | 69 +++++++++++++++++++ 2 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java diff --git a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java index abfdd006879e..1359812555df 100644 --- a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java +++ b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java @@ -3,14 +3,39 @@ import java.util.ArrayList; import java.util.List; +/** + * Utility class to print a matrix in spiral order. + *

+ * Given a 2D array (matrix), this class provides a method to return the + * elements + * of the matrix in spiral order, starting from the top-left corner and moving + * clockwise. + *

+ * + * @author Sadiul Hakim (https://github.com/sadiul-hakim) + */ public class PrintAMatrixInSpiralOrder { + /** - * Search a key in row and column wise sorted matrix + * Returns the elements of the given matrix in spiral order. + * + * @param matrix the 2D array to traverse in spiral order + * @param row the number of rows in the matrix + * @param col the number of columns in the matrix + * @return a list containing the elements of the matrix in spiral order + * + *

+ * Example: * - * @param matrix matrix to be searched - * @param row number of rows matrix has - * @param col number of columns matrix has - * @author Sadiul Hakim : https://github.com/sadiul-hakim + *

+     * int[][] matrix = {
+     *   {1, 2, 3},
+     *   {4, 5, 6},
+     *   {7, 8, 9}
+     * };
+     * print(matrix, 3, 3) returns [1, 2, 3, 6, 9, 8, 7, 4, 5]
+     *         
+ *

*/ public List print(int[][] matrix, int row, int col) { // r traverses matrix row wise from first diff --git a/src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java b/src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java new file mode 100644 index 000000000000..10ebc8114d05 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java @@ -0,0 +1,69 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.Test; + +class PrintAMatrixInSpiralOrderTest { + + private final PrintAMatrixInSpiralOrder spiralPrinter = new PrintAMatrixInSpiralOrder(); + + @Test + void testSquareMatrix() { + int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + List expected = Arrays.asList(1, 2, 3, 6, 9, 8, 7, 4, 5); + assertEquals(expected, spiralPrinter.print(matrix, 3, 3)); + } + + @Test + void testRectangularMatrixMoreRows() { + int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; + List expected = Arrays.asList(1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8); + assertEquals(expected, spiralPrinter.print(matrix, 4, 3)); + } + + @Test + void testRectangularMatrixMoreCols() { + int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; + List expected = Arrays.asList(1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7); + assertEquals(expected, spiralPrinter.print(matrix, 3, 4)); + } + + @Test + void testSingleRow() { + int[][] matrix = {{1, 2, 3, 4}}; + List expected = Arrays.asList(1, 2, 3, 4); + assertEquals(expected, spiralPrinter.print(matrix, 1, 4)); + } + + @Test + void testSingleColumn() { + int[][] matrix = {{1}, {2}, {3}}; + List expected = Arrays.asList(1, 2, 3); + assertEquals(expected, spiralPrinter.print(matrix, 3, 1)); + } + + @Test + void testEmptyMatrix() { + int[][] matrix = new int[0][0]; + List expected = Collections.emptyList(); + assertEquals(expected, spiralPrinter.print(matrix, 0, 0)); + } + + @Test + void testOneElementMatrix() { + int[][] matrix = {{42}}; + List expected = Collections.singletonList(42); + assertEquals(expected, spiralPrinter.print(matrix, 1, 1)); + } + + @Test + void testMatrixWithNegativeNumbers() { + int[][] matrix = {{-1, -2}, {-3, -4}}; + List expected = Arrays.asList(-1, -2, -4, -3); + assertEquals(expected, spiralPrinter.print(matrix, 2, 2)); + } +} From 015eb8505dcfac3efa89d9e4db7e5f092d1d3e51 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 5 Oct 2025 13:14:09 +0530 Subject: [PATCH 2/5] Fix error in BloomFilter --- .../bloomfilter/BloomFilter.java | 56 +++++++++++++++---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java index d60b95110fc2..639e995924a4 100644 --- a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java +++ b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java @@ -5,8 +5,10 @@ /** * A generic BloomFilter implementation for probabilistic membership checking. *

- * Bloom filters are space-efficient data structures that provide a fast way to test whether an - * element is a member of a set. They may produce false positives, indicating an element is + * Bloom filters are space-efficient data structures that provide a fast way to + * test whether an + * element is a member of a set. They may produce false positives, indicating an + * element is * in the set when it is not, but they will never produce false negatives. *

* @@ -20,11 +22,14 @@ public class BloomFilter { private final Hash[] hashFunctions; /** - * Constructs a BloomFilter with a specified number of hash functions and bit array size. + * Constructs a BloomFilter with a specified number of hash functions and bit + * array size. * * @param numberOfHashFunctions the number of hash functions to use - * @param bitArraySize the size of the bit array, which determines the capacity of the filter - * @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is less than 1 + * @param bitArraySize the size of the bit array, which determines the + * capacity of the filter + * @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is + * less than 1 */ @SuppressWarnings("unchecked") public BloomFilter(int numberOfHashFunctions, int bitArraySize) { @@ -38,7 +43,8 @@ public BloomFilter(int numberOfHashFunctions, int bitArraySize) { } /** - * Initializes the hash functions with unique indices to ensure different hashing. + * Initializes the hash functions with unique indices to ensure different + * hashing. */ private void initializeHashFunctions() { for (int i = 0; i < numberOfHashFunctions; i++) { @@ -49,7 +55,8 @@ private void initializeHashFunctions() { /** * Inserts an element into the Bloom filter. *

- * This method hashes the element using all defined hash functions and sets the corresponding + * This method hashes the element using all defined hash functions and sets the + * corresponding * bits in the bit array. *

* @@ -65,13 +72,16 @@ public void insert(T key) { /** * Checks if an element might be in the Bloom filter. *

- * This method checks the bits at the positions computed by each hash function. If any of these - * bits are not set, the element is definitely not in the filter. If all bits are set, the element + * This method checks the bits at the positions computed by each hash function. + * If any of these + * bits are not set, the element is definitely not in the filter. If all bits + * are set, the element * might be in the filter. *

* * @param key the element to check for membership in the Bloom filter - * @return {@code true} if the element might be in the Bloom filter, {@code false} if it is definitely not + * @return {@code true} if the element might be in the Bloom filter, + * {@code false} if it is definitely not */ public boolean contains(T key) { for (Hash hash : hashFunctions) { @@ -86,7 +96,8 @@ public boolean contains(T key) { /** * Inner class representing a hash function used by the Bloom filter. *

- * Each instance of this class represents a different hash function based on its index. + * Each instance of this class represents a different hash function based on its + * index. *

* * @param The type of elements to be hashed. @@ -115,7 +126,28 @@ private static class Hash { * @return the computed hash value */ public int compute(T key) { - return index * asciiString(String.valueOf(key)); + return index * asciiString(objectToString(key)); + } + + /** + * Converts the key to a string suitable for hashing. Handles arrays properly. + */ + private String objectToString(Object key) { + if (key == null) return "null"; + Class clazz = key.getClass(); + if (clazz.isArray()) { + if (clazz == byte[].class) return java.util.Arrays.toString((byte[]) key); + if (clazz == short[].class) return java.util.Arrays.toString((short[]) key); + if (clazz == int[].class) return java.util.Arrays.toString((int[]) key); + if (clazz == long[].class) return java.util.Arrays.toString((long[]) key); + if (clazz == char[].class) return java.util.Arrays.toString((char[]) key); + if (clazz == float[].class) return java.util.Arrays.toString((float[]) key); + if (clazz == double[].class) return java.util.Arrays.toString((double[]) key); + if (clazz == boolean[].class) return java.util.Arrays.toString((boolean[]) key); + // For object arrays or multi-dimensional arrays + return java.util.Arrays.deepToString((Object[]) key); + } + return String.valueOf(key); } /** From e1aff29ecc60f894fd1b4bd6deefd2799d63378b Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 5 Oct 2025 13:20:03 +0530 Subject: [PATCH 3/5] Fix --- .../com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java b/src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java index 10ebc8114d05..24debfe45c81 100644 --- a/src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java +++ b/src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java @@ -1,6 +1,6 @@ package com.thealgorithms.others; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.Collections; From abe61e776625c1b37710876753019c730f14b43d Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 5 Oct 2025 13:27:15 +0530 Subject: [PATCH 4/5] Fix --- .../bloomfilter/BloomFilter.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java index 639e995924a4..57d74eeb599d 100644 --- a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java +++ b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java @@ -133,17 +133,35 @@ public int compute(T key) { * Converts the key to a string suitable for hashing. Handles arrays properly. */ private String objectToString(Object key) { - if (key == null) return "null"; + if (key == null) { + return "null"; + } Class clazz = key.getClass(); if (clazz.isArray()) { - if (clazz == byte[].class) return java.util.Arrays.toString((byte[]) key); - if (clazz == short[].class) return java.util.Arrays.toString((short[]) key); - if (clazz == int[].class) return java.util.Arrays.toString((int[]) key); - if (clazz == long[].class) return java.util.Arrays.toString((long[]) key); - if (clazz == char[].class) return java.util.Arrays.toString((char[]) key); - if (clazz == float[].class) return java.util.Arrays.toString((float[]) key); - if (clazz == double[].class) return java.util.Arrays.toString((double[]) key); - if (clazz == boolean[].class) return java.util.Arrays.toString((boolean[]) key); + if (clazz == byte[].class) { + return java.util.Arrays.toString((byte[]) key); + } + if (clazz == short[].class) { + return java.util.Arrays.toString((short[]) key); + } + if (clazz == int[].class) { + return java.util.Arrays.toString((int[]) key); + } + if (clazz == long[].class) { + return java.util.Arrays.toString((long[]) key); + } + if (clazz == char[].class) { + return java.util.Arrays.toString((char[]) key); + } + if (clazz == float[].class) { + return java.util.Arrays.toString((float[]) key); + } + if (clazz == double[].class) { + return java.util.Arrays.toString((double[]) key); + } + if (clazz == boolean[].class) { + return java.util.Arrays.toString((boolean[]) key); + } // For object arrays or multi-dimensional arrays return java.util.Arrays.deepToString((Object[]) key); } From 16d066cf4f05c109386bd910868661439f3e457f Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 12 Oct 2025 11:32:38 +0530 Subject: [PATCH 5/5] Fix --- .../matrix/PrintAMatrixInSpiralOrder.java | 36 +++++++-- .../others/PrintAMatrixInSpiralOrder.java | 77 ------------------- .../PrintAMatrixInSpiralOrderTest.java | 16 +++- .../matrix/TestPrintMatrixInSpiralOrder.java | 26 ------- .../others/TestPrintMatrixInSpiralOrder.java | 26 ------- 5 files changed, 45 insertions(+), 136 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java rename src/test/java/com/thealgorithms/{others => matrix}/PrintAMatrixInSpiralOrderTest.java (77%) delete mode 100644 src/test/java/com/thealgorithms/matrix/TestPrintMatrixInSpiralOrder.java delete mode 100644 src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java diff --git a/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java index 2757da1f9023..4ae5970a9574 100644 --- a/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java +++ b/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java @@ -3,17 +3,41 @@ import java.util.ArrayList; import java.util.List; +/** + * Utility class to print a matrix in spiral order. + *

+ * Given a 2D array (matrix), this class provides a method to return the + * elements + * of the matrix in spiral order, starting from the top-left corner and moving + * clockwise. + *

+ * + * @author Sadiul Hakim (https://github.com/sadiul-hakim) + */ public class PrintAMatrixInSpiralOrder { + /** - * Search a key in row and column wise sorted matrix + * Returns the elements of the given matrix in spiral order. + * + * @param matrix the 2D array to traverse in spiral order + * @param row the number of rows in the matrix + * @param col the number of columns in the matrix + * @return a list containing the elements of the matrix in spiral order * - * @param matrix matrix to be searched - * @param row number of rows matrix has - * @param col number of columns matrix has - * @author Sadiul Hakim : https://github.com/sadiul-hakim + *

+ * Example: + * + *

+     * int[][] matrix = {
+     *   {1, 2, 3},
+     *   {4, 5, 6},
+     *   {7, 8, 9}
+     * };
+     * print(matrix, 3, 3) returns [1, 2, 3, 6, 9, 8, 7, 4, 5]
+     *         
+ *

*/ public List print(int[][] matrix, int row, int col) { - // r traverses matrix row wise from first int r = 0; // c traverses matrix column wise from first diff --git a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java deleted file mode 100644 index 1359812555df..000000000000 --- a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.thealgorithms.others; - -import java.util.ArrayList; -import java.util.List; - -/** - * Utility class to print a matrix in spiral order. - *

- * Given a 2D array (matrix), this class provides a method to return the - * elements - * of the matrix in spiral order, starting from the top-left corner and moving - * clockwise. - *

- * - * @author Sadiul Hakim (https://github.com/sadiul-hakim) - */ -public class PrintAMatrixInSpiralOrder { - - /** - * Returns the elements of the given matrix in spiral order. - * - * @param matrix the 2D array to traverse in spiral order - * @param row the number of rows in the matrix - * @param col the number of columns in the matrix - * @return a list containing the elements of the matrix in spiral order - * - *

- * Example: - * - *

-     * int[][] matrix = {
-     *   {1, 2, 3},
-     *   {4, 5, 6},
-     *   {7, 8, 9}
-     * };
-     * print(matrix, 3, 3) returns [1, 2, 3, 6, 9, 8, 7, 4, 5]
-     *         
- *

- */ - public List print(int[][] matrix, int row, int col) { - // r traverses matrix row wise from first - int r = 0; - // c traverses matrix column wise from first - int c = 0; - int i; - List result = new ArrayList<>(); - while (r < row && c < col) { - // print first row of matrix - for (i = c; i < col; i++) { - result.add(matrix[r][i]); - } - // increase r by one because first row printed - r++; - // print last column - for (i = r; i < row; i++) { - result.add(matrix[i][col - 1]); - } - // decrease col by one because last column has been printed - col--; - // print rows from last except printed elements - if (r < row) { - for (i = col - 1; i >= c; i--) { - result.add(matrix[row - 1][i]); - } - row--; - } - // print columns from first except printed elements - if (c < col) { - for (i = row - 1; i >= r; i--) { - result.add(matrix[i][c]); - } - c++; - } - } - return result; - } -} diff --git a/src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java b/src/test/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrderTest.java similarity index 77% rename from src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java rename to src/test/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrderTest.java index 24debfe45c81..745fdf98f193 100644 --- a/src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java +++ b/src/test/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrderTest.java @@ -1,4 +1,4 @@ -package com.thealgorithms.others; +package com.thealgorithms.matrix; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -66,4 +66,18 @@ void testMatrixWithNegativeNumbers() { List expected = Arrays.asList(-1, -2, -4, -3); assertEquals(expected, spiralPrinter.print(matrix, 2, 2)); } + + @Test + void testLargeSquareMatrix() { + 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}}; + List 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); + assertEquals(expected, spiralPrinter.print(matrix, 5, 5)); + } + + @Test + void testSingleRowWithTwoElements() { + int[][] matrix = {{2, 2}}; + List expected = Arrays.asList(2, 2); + assertEquals(expected, spiralPrinter.print(matrix, 1, 2)); + } } diff --git a/src/test/java/com/thealgorithms/matrix/TestPrintMatrixInSpiralOrder.java b/src/test/java/com/thealgorithms/matrix/TestPrintMatrixInSpiralOrder.java deleted file mode 100644 index bb415a5861a8..000000000000 --- a/src/test/java/com/thealgorithms/matrix/TestPrintMatrixInSpiralOrder.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.thealgorithms.matrix; - -import static org.junit.jupiter.api.Assertions.assertIterableEquals; - -import java.util.List; -import org.junit.jupiter.api.Test; - -public class TestPrintMatrixInSpiralOrder { - @Test - public void testOne() { - 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}}; - var printClass = new PrintAMatrixInSpiralOrder(); - List res = printClass.print(matrix, matrix.length, matrix[0].length); - List list = List.of(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); - assertIterableEquals(res, list); - } - - @Test - public void testTwo() { - int[][] matrix = {{2, 2}}; - var printClass = new PrintAMatrixInSpiralOrder(); - List res = printClass.print(matrix, matrix.length, matrix[0].length); - List list = List.of(2, 2); - assertIterableEquals(res, list); - } -} diff --git a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java deleted file mode 100644 index d35d4bb60c73..000000000000 --- a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.thealgorithms.others; - -import static org.junit.jupiter.api.Assertions.assertIterableEquals; - -import java.util.List; -import org.junit.jupiter.api.Test; - -public class TestPrintMatrixInSpiralOrder { - @Test - public void testOne() { - 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}}; - var printClass = new PrintAMatrixInSpiralOrder(); - List res = printClass.print(matrix, matrix.length, matrix[0].length); - List list = List.of(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); - assertIterableEquals(res, list); - } - - @Test - public void testTwo() { - int[][] matrix = {{2, 2}}; - var printClass = new PrintAMatrixInSpiralOrder(); - List res = printClass.print(matrix, matrix.length, matrix[0].length); - List list = List.of(2, 2); - assertIterableEquals(res, list); - } -}