Skip to content

Commit 50343c0

Browse files
authored
Merge branch 'master' into SMJI/stack/min_max_const_time
2 parents 405ecce + bcf4034 commit 50343c0

File tree

7 files changed

+145
-95
lines changed

7 files changed

+145
-95
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,11 +1014,14 @@
10141014
* [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java)
10151015
* [InverseOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/InverseOfMatrixTest.java)
10161016
* [MapReduceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MapReduceTest.java)
1017+
* [MatrixTransposeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java)
10171018
* [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java)
10181019
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
10191020
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
10201021
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
1022+
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
10211023
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
1024+
* [WordBoggleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/WordBoggleTest.java)
10221025
* others
10231026
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)
10241027
* [ArrayRightRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayRightRotationTest.java)
Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.misc;
22

3-
import java.util.Scanner;
4-
53
/**
64
*
75
*
@@ -22,62 +20,27 @@ public final class MatrixTranspose {
2220
private MatrixTranspose() {
2321
}
2422

25-
public static void main(String[] args) {
26-
/*
27-
* This is the main method
28-
*
29-
* @param args Unused.
30-
*
31-
* @return Nothing.
32-
*/
33-
Scanner sc = new Scanner(System.in);
34-
int i;
35-
int j;
36-
int row;
37-
int column;
38-
System.out.println("Enter the number of rows in the 2D matrix:");
39-
40-
/*
41-
* Take input from user for how many rows to be print
42-
*/
43-
row = sc.nextInt();
44-
45-
System.out.println("Enter the number of columns in the 2D matrix:");
46-
47-
/*
48-
* Take input from user for how many coloumn to be print
49-
*/
50-
column = sc.nextInt();
51-
int[][] arr = new int[row][column];
52-
System.out.println("Enter the elements");
53-
for (i = 0; i < row; i++) {
54-
for (j = 0; j < column; j++) {
55-
arr[i][j] = sc.nextInt();
56-
}
57-
}
58-
59-
/*
60-
* Print matrix before the Transpose in proper way
61-
*/
62-
System.out.println("The matrix is:");
63-
for (i = 0; i < row; i++) {
64-
for (j = 0; j < column; j++) {
65-
System.out.print(arr[i][j] + "\t");
66-
}
67-
System.out.print("\n");
23+
/**
24+
* Calculate the transpose of the given matrix.
25+
*
26+
* @param matrix The matrix to be transposed
27+
* @throws IllegalArgumentException if the matrix is empty
28+
* @throws NullPointerException if the matrix is null
29+
* @return The transposed matrix
30+
*/
31+
public static int[][] transpose(int[][] matrix) {
32+
if (matrix == null || matrix.length == 0) {
33+
throw new IllegalArgumentException("Matrix is empty");
6834
}
6935

70-
/*
71-
* Print matrix after the tranpose in proper way Transpose means Interchanging
72-
* of rows wth column so we interchange the rows in next loop Thus at last
73-
* matrix of transpose is obtained through user input...
74-
*/
75-
System.out.println("The Transpose of the given matrix is:");
76-
for (i = 0; i < column; i++) {
77-
for (j = 0; j < row; j++) {
78-
System.out.print(arr[j][i] + "\t");
36+
int rows = matrix.length;
37+
int cols = matrix[0].length;
38+
int[][] transposedMatrix = new int[cols][rows];
39+
for (int i = 0; i < cols; i++) {
40+
for (int j = 0; j < rows; j++) {
41+
transposedMatrix[i][j] = matrix[j][i];
7942
}
80-
System.out.print("\n");
8143
}
44+
return transposedMatrix;
8245
}
8346
}

src/main/java/com/thealgorithms/misc/RangeInSortedArray.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
package com.thealgorithms.misc;
22

3-
import java.util.Arrays;
4-
53
public final class RangeInSortedArray {
64
private RangeInSortedArray() {
75
}
86

9-
public static void main(String[] args) {
10-
// Testcases
11-
assert Arrays.equals(sortedRange(new int[] {1, 2, 3, 3, 3, 4, 5}, 3), new int[] {2, 4});
12-
assert Arrays.equals(sortedRange(new int[] {1, 2, 3, 3, 3, 4, 5}, 4), new int[] {5, 5});
13-
assert Arrays.equals(sortedRange(new int[] {0, 1, 2}, 3), new int[] {-1, -1});
14-
}
15-
167
// Get the 1st and last occurrence index of a number 'key' in a non-decreasing array 'nums'
178
// Gives [-1, -1] in case element doesn't exist in array
189
public static int[] sortedRange(int[] nums, int key) {

src/main/java/com/thealgorithms/misc/WordBoggle.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.thealgorithms.misc;
22

33
import java.util.ArrayList;
4-
import java.util.Arrays;
54
import java.util.HashMap;
65
import java.util.HashSet;
76
import java.util.List;
@@ -32,36 +31,6 @@ public static List<String> boggleBoard(char[][] board, String[] words) {
3231
return new ArrayList<>(finalWords);
3332
}
3433

35-
public static void main(String[] args) {
36-
// Testcase
37-
List<String> ans = new ArrayList<>(Arrays.asList("a", "boggle", "this", "NOTRE_PEATED", "is", "simple", "board"));
38-
assert (boggleBoard(
39-
new char[][] {
40-
{'t', 'h', 'i', 's', 'i', 's', 'a'},
41-
{'s', 'i', 'm', 'p', 'l', 'e', 'x'},
42-
{'b', 'x', 'x', 'x', 'x', 'e', 'b'},
43-
{'x', 'o', 'g', 'g', 'l', 'x', 'o'},
44-
{'x', 'x', 'x', 'D', 'T', 'r', 'a'},
45-
{'R', 'E', 'P', 'E', 'A', 'd', 'x'},
46-
{'x', 'x', 'x', 'x', 'x', 'x', 'x'},
47-
{'N', 'O', 'T', 'R', 'E', '_', 'P'},
48-
{'x', 'x', 'D', 'E', 'T', 'A', 'E'},
49-
},
50-
new String[] {
51-
"this",
52-
"is",
53-
"not",
54-
"a",
55-
"simple",
56-
"test",
57-
"boggle",
58-
"board",
59-
"REPEATED",
60-
"NOTRE_PEATED",
61-
})
62-
.equals(ans));
63-
}
64-
6534
public static void explore(int i, int j, char[][] board, TrieNode trieNode, boolean[][] visited, Set<String> finalWords) {
6635
if (visited[i][j]) {
6736
return;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
public class MatrixTransposeTest {
12+
13+
private static Stream<Arguments> provideValidMatrixTestCases() {
14+
return Stream.of(Arguments.of(new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, new int[][] {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}, "Transpose of square matrix"), Arguments.of(new int[][] {{1, 2}, {3, 4}, {5, 6}}, new int[][] {{1, 3, 5}, {2, 4, 6}}, "Transpose of rectangular matrix"),
15+
Arguments.of(new int[][] {{1, 2, 3}}, new int[][] {{1}, {2}, {3}}, "Transpose of single-row matrix"), Arguments.of(new int[][] {{1}, {2}, {3}}, new int[][] {{1, 2, 3}}, "Transpose of single-column matrix"));
16+
}
17+
18+
private static Stream<Arguments> provideInvalidMatrixTestCases() {
19+
return Stream.of(Arguments.of(new int[0][0], "Empty matrix should throw IllegalArgumentException"), Arguments.of(null, "Null matrix should throw IllegalArgumentException"));
20+
}
21+
22+
@ParameterizedTest(name = "Test case {index}: {2}")
23+
@MethodSource("provideValidMatrixTestCases")
24+
void testValidMatrixTranspose(int[][] input, int[][] expected, String description) {
25+
assertArrayEquals(expected, MatrixTranspose.transpose(input), description);
26+
}
27+
28+
@ParameterizedTest(name = "Test case {index}: {1}")
29+
@MethodSource("provideInvalidMatrixTestCases")
30+
void testInvalidMatrixTranspose(int[][] input, String description) {
31+
assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(input), description);
32+
}
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
public class RangeInSortedArrayTest {
12+
13+
@ParameterizedTest(name = "Test case {index}: {3}")
14+
@MethodSource("provideSortedRangeTestCases")
15+
void testSortedRange(int[] nums, int key, int[] expectedRange, String description) {
16+
assertArrayEquals(expectedRange, RangeInSortedArray.sortedRange(nums, key), description);
17+
}
18+
19+
private static Stream<Arguments> provideSortedRangeTestCases() {
20+
return Stream.of(Arguments.of(new int[] {1, 2, 3, 3, 3, 4, 5}, 3, new int[] {2, 4}, "Range for key 3 with multiple occurrences"), Arguments.of(new int[] {1, 2, 3, 3, 3, 4, 5}, 4, new int[] {5, 5}, "Range for key 4 with single occurrence"),
21+
Arguments.of(new int[] {0, 1, 2}, 3, new int[] {-1, -1}, "Range for non-existent key"), Arguments.of(new int[] {}, 1, new int[] {-1, -1}, "Range in empty array"), Arguments.of(new int[] {1, 1, 1, 2, 3, 4, 5, 5, 5}, 1, new int[] {0, 2}, "Range for key at start"),
22+
Arguments.of(new int[] {1, 1, 1, 2, 3, 4, 5, 5, 5}, 5, new int[] {6, 8}, "Range for key at end"));
23+
}
24+
25+
@ParameterizedTest(name = "Test case {index}: {3}")
26+
@MethodSource("provideGetCountLessThanTestCases")
27+
void testGetCountLessThan(int[] nums, int key, int expectedCount, String description) {
28+
assertEquals(expectedCount, RangeInSortedArray.getCountLessThan(nums, key), description);
29+
}
30+
31+
private static Stream<Arguments> provideGetCountLessThanTestCases() {
32+
return Stream.of(Arguments.of(new int[] {1, 2, 3, 3, 4, 5}, 3, 4, "Count of elements less than existing key"), Arguments.of(new int[] {1, 2, 3, 3, 4, 5}, 4, 5, "Count of elements less than non-existing key"), Arguments.of(new int[] {1, 2, 2, 3}, 5, 4, "Count with all smaller elements"),
33+
Arguments.of(new int[] {2, 3, 4, 5}, 1, 0, "Count with no smaller elements"), Arguments.of(new int[] {}, 1, 0, "Count in empty array"));
34+
}
35+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.stream.Stream;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.Arguments;
12+
import org.junit.jupiter.params.provider.MethodSource;
13+
14+
class WordBoggleTest {
15+
private char[][] board;
16+
17+
@BeforeEach
18+
void setup() {
19+
board = new char[][] {
20+
{'t', 'h', 'i', 's', 'i', 's', 'a'},
21+
{'s', 'i', 'm', 'p', 'l', 'e', 'x'},
22+
{'b', 'x', 'x', 'x', 'x', 'e', 'b'},
23+
{'x', 'o', 'g', 'g', 'l', 'x', 'o'},
24+
{'x', 'x', 'x', 'D', 'T', 'r', 'a'},
25+
{'R', 'E', 'P', 'E', 'A', 'd', 'x'},
26+
{'x', 'x', 'x', 'x', 'x', 'x', 'x'},
27+
{'N', 'O', 'T', 'R', 'E', '_', 'P'},
28+
{'x', 'x', 'D', 'E', 'T', 'A', 'E'},
29+
};
30+
}
31+
32+
@ParameterizedTest
33+
@MethodSource("provideTestCases")
34+
void testBoggleBoard(String[] words, List<String> expectedWords, String testDescription) {
35+
List<String> result = WordBoggle.boggleBoard(board, words);
36+
assertEquals(expectedWords.size(), result.size(), "Test failed for: " + testDescription);
37+
assertTrue(expectedWords.containsAll(result), "Test failed for: " + testDescription);
38+
}
39+
40+
private static Stream<Arguments> provideTestCases() {
41+
return Stream.of(Arguments.of(new String[] {"this", "is", "not", "a", "simple", "test", "boggle", "board", "REPEATED", "NOTRE_PEATED"}, Arrays.asList("this", "is", "a", "simple", "board", "boggle", "NOTRE_PEATED"), "All words"),
42+
Arguments.of(new String[] {"xyz", "hello", "world"}, List.of(), "No matching words"), Arguments.of(new String[] {}, List.of(), "Empty words array"), Arguments.of(new String[] {"this", "this", "board", "board"}, Arrays.asList("this", "board"), "Duplicate words in input"));
43+
}
44+
45+
@ParameterizedTest
46+
@MethodSource("provideSpecialCases")
47+
void testBoggleBoardSpecialCases(char[][] specialBoard, String[] words, List<String> expectedWords, String testDescription) {
48+
List<String> result = WordBoggle.boggleBoard(specialBoard, words);
49+
assertEquals(expectedWords.size(), result.size(), "Test failed for: " + testDescription);
50+
assertTrue(expectedWords.containsAll(result), "Test failed for: " + testDescription);
51+
}
52+
53+
private static Stream<Arguments> provideSpecialCases() {
54+
return Stream.of(Arguments.of(new char[0][0], new String[] {"this", "is", "a", "test"}, List.of(), "Empty board"));
55+
}
56+
}

0 commit comments

Comments
 (0)