Skip to content

Commit 32e2191

Browse files
committed
refactor: Enhance docs, add tests in PrintMatrixInSpiralOrder
1 parent a0b6c52 commit 32e2191

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,39 @@
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
26+
*
27+
* <p>
28+
* Example:
929
*
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
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) {
1641
// r traverses matrix row wise from first
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
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+
}

0 commit comments

Comments
 (0)