Add two algorithms with matrixes (#3364)

This commit is contained in:
sadiul-hakim☪️
2023-01-14 16:01:03 +06:00
committed by GitHub
parent 351e85d264
commit d5f140458a
4 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package com.thealgorithms.others;
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
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<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
List<Integer> 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<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
List<Integer> list = List.of(2, 2);
assertIterableEquals(res, list);
}
}

View File

@ -0,0 +1,38 @@
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class TestSearchInARowAndColWiseSortedMatrix {
@Test
public void searchItem() {
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 test = new SearchInARowAndColWiseSortedMatrix();
int[] res = test.search(matrix, 16);
int[] expectedResult = { 2, 2 };
assertArrayEquals(expectedResult, res);
}
@Test
public void notFound() {
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 test = new SearchInARowAndColWiseSortedMatrix();
int[] res = test.search(matrix, 96);
int[] expectedResult = { -1, -1 };
assertArrayEquals(expectedResult, res);
}
}