Skip to content

Commit f06cc60

Browse files
authored
Merge branch 'master' into refactor/find_kth_number
2 parents 7e4b666 + 84fb717 commit f06cc60

File tree

6 files changed

+165
-78
lines changed

6 files changed

+165
-78
lines changed

src/main/java/com/thealgorithms/backtracking/NQueens.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,10 @@ public final class NQueens {
3636
private NQueens() {
3737
}
3838

39-
public static void main(String[] args) {
40-
placeQueens(1);
41-
placeQueens(2);
42-
placeQueens(3);
43-
placeQueens(4);
44-
placeQueens(5);
45-
placeQueens(6);
39+
public static List<List<String>> getNQueensArrangements(int queens) {
40+
List<List<String>> arrangements = new ArrayList<>();
41+
getSolution(queens, arrangements, new int[queens], 0);
42+
return arrangements;
4643
}
4744

4845
public static void placeQueens(final int queens) {
Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Converts any Hexadecimal Number to Octal
75
*
@@ -12,64 +10,53 @@ private HexToOct() {
1210
}
1311

1412
/**
15-
* This method converts a Hexadecimal number to a decimal number
13+
* Converts a Hexadecimal number to a Decimal number.
1614
*
17-
* @param s The Hexadecimal Number
18-
* @return The Decimal number
15+
* @param hex The Hexadecimal number as a String.
16+
* @return The Decimal equivalent as an integer.
1917
*/
20-
public static int hex2decimal(String s) {
21-
String str = "0123456789ABCDEF";
22-
s = s.toUpperCase();
23-
int val = 0;
24-
for (int i = 0; i < s.length(); i++) {
25-
char a = s.charAt(i);
26-
int n = str.indexOf(a);
27-
val = 16 * val + n;
18+
public static int hexToDecimal(String hex) {
19+
String hexDigits = "0123456789ABCDEF";
20+
hex = hex.toUpperCase();
21+
int decimalValue = 0;
22+
23+
for (int i = 0; i < hex.length(); i++) {
24+
char hexChar = hex.charAt(i);
25+
int digitValue = hexDigits.indexOf(hexChar);
26+
decimalValue = 16 * decimalValue + digitValue;
2827
}
29-
return val;
28+
29+
return decimalValue;
3030
}
3131

3232
/**
33-
* This method converts a Decimal number to a octal number
33+
* Converts a Decimal number to an Octal number.
3434
*
35-
* @param q The Decimal Number
36-
* @return The Octal number
35+
* @param decimal The Decimal number as an integer.
36+
* @return The Octal equivalent as an integer.
3737
*/
38-
public static int decimal2octal(int q) {
39-
int now;
40-
int i = 1;
41-
int octnum = 0;
42-
while (q > 0) {
43-
now = q % 8;
44-
octnum = (now * (int) (Math.pow(10, i))) + octnum;
45-
q /= 8;
46-
i++;
38+
public static int decimalToOctal(int decimal) {
39+
int octalValue = 0;
40+
int placeValue = 1;
41+
42+
while (decimal > 0) {
43+
int remainder = decimal % 8;
44+
octalValue += remainder * placeValue;
45+
decimal /= 8;
46+
placeValue *= 10;
4747
}
48-
octnum /= 10;
49-
return octnum;
48+
49+
return octalValue;
5050
}
5151

5252
/**
53-
* Main method that gets the hex input from user and converts it into octal.
53+
* Converts a Hexadecimal number to an Octal number.
5454
*
55-
* @param args arguments
55+
* @param hex The Hexadecimal number as a String.
56+
* @return The Octal equivalent as an integer.
5657
*/
57-
public static void main(String[] args) {
58-
String hexadecnum;
59-
int decnum;
60-
int octalnum;
61-
Scanner scan = new Scanner(System.in);
62-
63-
System.out.print("Enter Hexadecimal Number : ");
64-
hexadecnum = scan.nextLine();
65-
66-
// first convert hexadecimal to decimal
67-
decnum = hex2decimal(hexadecnum); // Pass the string to the hex2decimal function and get the decimal form in
68-
// variable decnum
69-
70-
// convert decimal to octal
71-
octalnum = decimal2octal(decnum);
72-
System.out.println("Number in octal: " + octalnum);
73-
scan.close();
58+
public static int hexToOctal(String hex) {
59+
int decimalValue = hexToDecimal(hex);
60+
return decimalToOctal(decimalValue);
7461
}
7562
}

src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.maths;
22

3-
import java.util.Scanner;
4-
53
/*
64
* @author Ojasva Jain
75
* Determinant of a Matrix Wikipedia link: https://en.wikipedia.org/wiki/Determinant
@@ -10,8 +8,13 @@ public final class DeterminantOfMatrix {
108
private DeterminantOfMatrix() {
119
}
1210

13-
// Determinant calculator
14-
//@return determinant of the input matrix
11+
/**
12+
* Calculates the determinant of a given matrix.
13+
*
14+
* @param a the input matrix
15+
* @param n the size of the matrix
16+
* @return the determinant of the matrix
17+
*/
1518
static int determinant(int[][] a, int n) {
1619
int det = 0;
1720
int sign = 1;
@@ -41,21 +44,4 @@ static int determinant(int[][] a, int n) {
4144
}
4245
return det;
4346
}
44-
45-
// Driver Method
46-
public static void main(String[] args) {
47-
Scanner in = new Scanner(System.in);
48-
// Input Matrix
49-
System.out.println("Enter matrix size (Square matrix only)");
50-
int n = in.nextInt();
51-
System.out.println("Enter matrix");
52-
int[][] a = new int[n][n];
53-
for (int i = 0; i < n; i++) {
54-
for (int j = 0; j < n; j++) {
55-
a[i][j] = in.nextInt();
56-
}
57-
}
58-
System.out.println(determinant(a, n));
59-
in.close();
60-
}
6147
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class NQueensTest {
11+
12+
@Test
13+
public void testNQueens1() {
14+
List<List<String>> expected = Arrays.asList(Arrays.asList("Q"));
15+
assertEquals(expected, NQueens.getNQueensArrangements(1));
16+
}
17+
18+
@Test
19+
public void testNQueens2() {
20+
List<List<String>> expected = new ArrayList<>(); // No solution exists
21+
assertEquals(expected, NQueens.getNQueensArrangements(2));
22+
}
23+
24+
@Test
25+
public void testNQueens3() {
26+
List<List<String>> expected = new ArrayList<>(); // No solution exists
27+
assertEquals(expected, NQueens.getNQueensArrangements(3));
28+
}
29+
30+
@Test
31+
public void testNQueens4() {
32+
List<List<String>> expected = Arrays.asList(Arrays.asList(".Q..", "...Q", "Q...", "..Q."), Arrays.asList("..Q.", "Q...", "...Q", ".Q.."));
33+
assertEquals(expected, NQueens.getNQueensArrangements(4));
34+
}
35+
36+
@Test
37+
public void testNQueens5() {
38+
// Only the number of solutions is tested for larger N due to the complexity of checking each board configuration
39+
List<List<String>> result = NQueens.getNQueensArrangements(5);
40+
assertEquals(10, result.size()); // 5x5 board has 10 solutions
41+
}
42+
43+
@Test
44+
public void testNQueens6() {
45+
List<List<String>> result = NQueens.getNQueensArrangements(6);
46+
assertEquals(4, result.size()); // 6x6 board has 4 solutions
47+
}
48+
}

src/test/java/com/thealgorithms/conversions/HexToOctTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,29 @@
55
import org.junit.jupiter.api.Test;
66

77
public class HexToOctTest {
8+
@Test
9+
public void testHexToDecimal() {
10+
assertEquals(255, HexToOct.hexToDecimal("FF"));
11+
assertEquals(16, HexToOct.hexToDecimal("10"));
12+
assertEquals(0, HexToOct.hexToDecimal("0"));
13+
assertEquals(4095, HexToOct.hexToDecimal("FFF"));
14+
}
15+
16+
@Test
17+
public void testDecimalToOctal() {
18+
assertEquals(110, HexToOct.decimalToOctal(HexToOct.hexToDecimal("48")));
19+
assertEquals(255, HexToOct.decimalToOctal(HexToOct.hexToDecimal("AD")));
20+
assertEquals(377, HexToOct.decimalToOctal(255));
21+
assertEquals(20, HexToOct.decimalToOctal(16));
22+
assertEquals(0, HexToOct.decimalToOctal(0));
23+
assertEquals(7777, HexToOct.decimalToOctal(4095));
24+
}
825

926
@Test
10-
public void testHexToOct() {
11-
assertEquals(110, HexToOct.decimal2octal(HexToOct.hex2decimal("48")));
12-
assertEquals(255, HexToOct.decimal2octal(HexToOct.hex2decimal("AD")));
27+
public void testHexToOctal() {
28+
assertEquals(377, HexToOct.hexToOctal("FF"));
29+
assertEquals(20, HexToOct.hexToOctal("10"));
30+
assertEquals(0, HexToOct.hexToOctal("0"));
31+
assertEquals(7777, HexToOct.hexToOctal("FFF"));
1332
}
1433
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class DeterminantOfMatrixTest {
8+
9+
@Test
10+
public void testDeterminant2x2Matrix() {
11+
int[][] matrix = {{1, 2}, {3, 4}};
12+
int expected = -2;
13+
assertEquals(expected, DeterminantOfMatrix.determinant(matrix, 2));
14+
}
15+
16+
@Test
17+
public void testDeterminant3x3Matrix() {
18+
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
19+
int expected = 0;
20+
assertEquals(expected, DeterminantOfMatrix.determinant(matrix, 3));
21+
}
22+
23+
@Test
24+
public void testDeterminant3x3MatrixNonZero() {
25+
int[][] matrix = {{1, 2, 3}, {0, 1, 4}, {5, 6, 0}};
26+
int expected = 1;
27+
assertEquals(expected, DeterminantOfMatrix.determinant(matrix, 3));
28+
}
29+
30+
@Test
31+
public void testDeterminant1x1Matrix() {
32+
int[][] matrix = {{7}};
33+
int expected = 7;
34+
assertEquals(expected, DeterminantOfMatrix.determinant(matrix, 1));
35+
}
36+
37+
@Test
38+
public void testDeterminant4x4Matrix() {
39+
int[][] matrix = {{1, 0, 0, 1}, {0, 1, 0, 0}, {0, 0, 1, 0}, {1, 0, 0, 1}};
40+
int expected = 0;
41+
assertEquals(expected, DeterminantOfMatrix.determinant(matrix, 4));
42+
}
43+
44+
@Test
45+
public void testDeterminant4x4MatrixZero() {
46+
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
47+
int expected = 0;
48+
assertEquals(expected, DeterminantOfMatrix.determinant(matrix, 4));
49+
}
50+
}

0 commit comments

Comments
 (0)