Added MirrorOfMatrix.java (#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>
This commit is contained in:
Aman 2023-10-03 22:12:56 +05:30 committed by GitHub
parent 9795bada90
commit 329cc3bcf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package com.thealgorithms.misc;
// Problem Statement
/*
We have given an array of m x n (where m is the number of rows and n is the number of columns).
Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
The Original matrix is: | The Mirror matrix is:
1 2 3 | 3 2 1
4 5 6 | 6 5 4
7 8 9 | 9 8 7
@author - Aman (https://github.com/Aman28801)
*/
public final class MirrorOfMatrix {
private MirrorOfMatrix() {
}
public static int[][] mirrorMatrix(final int[][] originalMatrix) {
if (originalMatrix == null) {
// Handle invalid input
return null;
}
if (originalMatrix.length == 0) {
return new int[0][0];
}
checkInput(originalMatrix);
int numRows = originalMatrix.length;
int numCols = originalMatrix[0].length;
int[][] mirroredMatrix = new int[numRows][numCols];
for (int i = 0; i < numRows; i++) {
mirroredMatrix[i] = reverseRow(originalMatrix[i]);
}
return mirroredMatrix;
}
private static int[] reverseRow(final int[] inRow) {
int[] res = new int[inRow.length];
for (int i = 0; i < inRow.length; ++i) {
res[i] = inRow[inRow.length - 1 - i];
}
return res;
}
private static void checkInput(final int[][] matrix) {
// Check if all rows have the same number of columns
for (int i = 1; i < matrix.length; i++) {
if (matrix[i].length != matrix[0].length) {
throw new IllegalArgumentException("The input is not a matrix.");
}
}
}
}

View File

@ -0,0 +1,53 @@
package com.thealgorithms.misc;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class MirrorOfMatrixTest {
@Test
void testMirrorMatrixRegularMatrix() {
int[][] originalMatrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] expectedMirrorMatrix = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
}
@Test
void testMirrorMatrixEmptyMatrix() {
int[][] originalMatrix = {};
int[][] expectedMirrorMatrix = {};
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
}
@Test
void testMirrorMatrixSingleElementMatrix() {
int[][] originalMatrix = {{42}};
int[][] expectedMirrorMatrix = {{42}};
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
}
@Test
void testMirrorMatrixMultipleRowsOneColumnMatrix() {
int[][] originalMatrix = {{1}, {2}, {3}, {4}};
int[][] expectedMirrorMatrix = {{1}, {2}, {3}, {4}};
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
}
@Test
void testMirrorMatrixNullInput() {
int[][] originalMatrix = null;
assertNull(MirrorOfMatrix.mirrorMatrix(originalMatrix));
}
@Test
void testMirrotMarixThrows() {
assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(new int[][] {{1}, {2, 3}}));
}
}