Skip to content

Commit 329cc3b

Browse files
Aman28801vil02
andauthored
Added MirrorOfMatrix.java (TheAlgorithms#4461)
* Added MirrorOfMatrix.java * Fixing Linting Issue * Fixing Linting Issue * Fixing Linting Issue * Fixing Linting Issue * Fixing Linting Issue * Fixing Linting Issue * Fixing Linting Issue * Fixing Linting Issue * Changes Done * Update src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Changes Done * Added MirrorOfMatrixTest.java * Added MirrorOfMatrixTest.java * Linting Error in Test * Linting Error in Test * Linting Error in Test * trying to fix build error * trying to fix build error * final * Update src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Changing Description * Final * Update src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Final * Update src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Changes * Changes * Linting Issue * Linting Issue * Linting Issue * Changes * Fixing Minor Linting Issue * Fixing Minor Linting Issue * Final * Update src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Changes * Linting Issue * Linting Issue * Linting Issue * Linting Issue * Update src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Changes * Changes * fix: use proper size in `checkInput` * style: basic linting --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> Co-authored-by: vil02 <vil02@o2.pl>
1 parent 9795bad commit 329cc3b

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.misc;
2+
3+
// Problem Statement
4+
/*
5+
We have given an array of m x n (where m is the number of rows and n is the number of columns).
6+
Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
7+
8+
The Original matrix is: | The Mirror matrix is:
9+
1 2 3 | 3 2 1
10+
4 5 6 | 6 5 4
11+
7 8 9 | 9 8 7
12+
13+
@author - Aman (https://github.com/Aman28801)
14+
*/
15+
16+
public final class MirrorOfMatrix {
17+
private MirrorOfMatrix() {
18+
}
19+
20+
public static int[][] mirrorMatrix(final int[][] originalMatrix) {
21+
if (originalMatrix == null) {
22+
// Handle invalid input
23+
return null;
24+
}
25+
if (originalMatrix.length == 0) {
26+
return new int[0][0];
27+
}
28+
29+
checkInput(originalMatrix);
30+
31+
int numRows = originalMatrix.length;
32+
int numCols = originalMatrix[0].length;
33+
34+
int[][] mirroredMatrix = new int[numRows][numCols];
35+
36+
for (int i = 0; i < numRows; i++) {
37+
mirroredMatrix[i] = reverseRow(originalMatrix[i]);
38+
}
39+
return mirroredMatrix;
40+
}
41+
private static int[] reverseRow(final int[] inRow) {
42+
int[] res = new int[inRow.length];
43+
for (int i = 0; i < inRow.length; ++i) {
44+
res[i] = inRow[inRow.length - 1 - i];
45+
}
46+
return res;
47+
}
48+
49+
private static void checkInput(final int[][] matrix) {
50+
// Check if all rows have the same number of columns
51+
for (int i = 1; i < matrix.length; i++) {
52+
if (matrix[i].length != matrix[0].length) {
53+
throw new IllegalArgumentException("The input is not a matrix.");
54+
}
55+
}
56+
}
57+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
class MirrorOfMatrixTest {
10+
11+
@Test
12+
void testMirrorMatrixRegularMatrix() {
13+
int[][] originalMatrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
14+
int[][] expectedMirrorMatrix = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
15+
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
16+
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
17+
}
18+
19+
@Test
20+
void testMirrorMatrixEmptyMatrix() {
21+
int[][] originalMatrix = {};
22+
int[][] expectedMirrorMatrix = {};
23+
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
24+
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
25+
}
26+
27+
@Test
28+
void testMirrorMatrixSingleElementMatrix() {
29+
int[][] originalMatrix = {{42}};
30+
int[][] expectedMirrorMatrix = {{42}};
31+
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
32+
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
33+
}
34+
35+
@Test
36+
void testMirrorMatrixMultipleRowsOneColumnMatrix() {
37+
int[][] originalMatrix = {{1}, {2}, {3}, {4}};
38+
int[][] expectedMirrorMatrix = {{1}, {2}, {3}, {4}};
39+
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
40+
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
41+
}
42+
43+
@Test
44+
void testMirrorMatrixNullInput() {
45+
int[][] originalMatrix = null;
46+
assertNull(MirrorOfMatrix.mirrorMatrix(originalMatrix));
47+
}
48+
49+
@Test
50+
void testMirrotMarixThrows() {
51+
assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(new int[][] {{1}, {2, 3}}));
52+
}
53+
}

0 commit comments

Comments
 (0)