Skip to content

Commit 07368ad

Browse files
Merge branch 'master' into patch-1
2 parents 91d3e88 + fa22317 commit 07368ad

File tree

202 files changed

+6865
-3766
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

202 files changed

+6865
-3766
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @yanglbme @vil02 @BamaCharanChhandogi @alxkm
1+
* @yanglbme @vil02 @BamaCharanChhandogi @alxkm @siriak

DIRECTORY.md

Lines changed: 75 additions & 19 deletions
Large diffs are not rendered by default.

pmd-exclude.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ com.thealgorithms.datastructures.crdt.LWWElementSet=UselessParentheses
1212
com.thealgorithms.datastructures.crdt.Pair=UnusedPrivateField
1313
com.thealgorithms.datastructures.graphs.AStar=UselessParentheses
1414
com.thealgorithms.datastructures.graphs.AdjacencyMatrixGraph=CollapsibleIfStatements,UnnecessaryFullyQualifiedName,UselessParentheses
15-
com.thealgorithms.datastructures.graphs.BipartiteGrapfDFS=CollapsibleIfStatements
15+
com.thealgorithms.datastructures.graphs.BipartiteGraphDFS=CollapsibleIfStatements
1616
com.thealgorithms.datastructures.graphs.Kruskal=UselessParentheses
1717
com.thealgorithms.datastructures.hashmap.hashing.HashMapCuckooHashing=UselessParentheses
1818
com.thealgorithms.datastructures.heaps.FibonacciHeap=UselessParentheses

pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>5.10.3</version>
23+
<version>5.11.0</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>org.junit.jupiter</groupId>
3333
<artifactId>junit-jupiter</artifactId>
34-
<version>5.10.3</version>
34+
<version>5.11.0</version>
3535
<scope>test</scope>
3636
</dependency>
3737
<dependency>
@@ -44,13 +44,13 @@
4444
<dependency>
4545
<groupId>org.junit.jupiter</groupId>
4646
<artifactId>junit-jupiter-api</artifactId>
47-
<version>5.10.3</version>
47+
<version>5.11.0</version>
4848
<scope>test</scope>
4949
</dependency>
5050
<dependency>
5151
<groupId>org.apache.commons</groupId>
5252
<artifactId>commons-lang3</artifactId>
53-
<version>3.16.0</version>
53+
<version>3.17.0</version>
5454
</dependency>
5555
<dependency>
5656
<groupId>org.apache.commons</groupId>
@@ -63,7 +63,7 @@
6363
<plugins>
6464
<plugin>
6565
<artifactId>maven-surefire-plugin</artifactId>
66-
<version>3.3.1</version>
66+
<version>3.5.0</version>
6767
<configuration>
6868
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
6969
</configuration>
@@ -107,7 +107,7 @@
107107
<plugin>
108108
<groupId>org.apache.maven.plugins</groupId>
109109
<artifactId>maven-checkstyle-plugin</artifactId>
110-
<version>3.4.0</version>
110+
<version>3.5.0</version>
111111
<configuration>
112112
<configLocation>checkstyle.xml</configLocation>
113113
<consoleOutput>true</consoleOutput>
@@ -118,14 +118,14 @@
118118
<dependency>
119119
<groupId>com.puppycrawl.tools</groupId>
120120
<artifactId>checkstyle</artifactId>
121-
<version>10.17.0</version>
121+
<version>10.18.1</version>
122122
</dependency>
123123
</dependencies>
124124
</plugin>
125125
<plugin>
126126
<groupId>com.github.spotbugs</groupId>
127127
<artifactId>spotbugs-maven-plugin</artifactId>
128-
<version>4.8.6.2</version>
128+
<version>4.8.6.3</version>
129129
<configuration>
130130
<excludeFilterFile>spotbugs-exclude.xml</excludeFilterFile>
131131
<includeTests>true</includeTests>
@@ -146,7 +146,7 @@
146146
<plugin>
147147
<groupId>org.apache.maven.plugins</groupId>
148148
<artifactId>maven-pmd-plugin</artifactId>
149-
<version>3.24.0</version>
149+
<version>3.25.0</version>
150150
<configuration>
151151
<printFailingErrors>true</printFailingErrors>
152152
<includeTests>true</includeTests>

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) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private ParenthesesGenerator() {
1919
*/
2020
public static List<String> generateParentheses(final int n) {
2121
if (n < 0) {
22-
throw new IllegalArgumentException("The number of pairs of parentheses cannot be nagative");
22+
throw new IllegalArgumentException("The number of pairs of parentheses cannot be negative");
2323
}
2424
List<String> result = new ArrayList<>();
2525
generateParenthesesHelper(result, "", 0, 0, n);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.conversions;
2+
3+
public final class AffineConverter {
4+
private final double slope;
5+
private final double intercept;
6+
public AffineConverter(final double inSlope, final double inIntercept) {
7+
slope = inSlope;
8+
intercept = inIntercept;
9+
}
10+
11+
public double convert(final double inValue) {
12+
return slope * inValue + intercept;
13+
}
14+
15+
public AffineConverter invert() {
16+
assert slope != 0.0;
17+
return new AffineConverter(1.0 / slope, -intercept / slope);
18+
}
19+
20+
public AffineConverter compose(final AffineConverter other) {
21+
return new AffineConverter(slope * other.slope, slope * other.intercept + intercept);
22+
}
23+
}

src/main/java/com/thealgorithms/conversions/AnyBaseToDecimal.java

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,50 @@
33
/**
44
* @author Varun Upadhyay (<a href="https://github.com/varunu28">...</a>)
55
*/
6-
// Driver program
76
public final class AnyBaseToDecimal {
8-
private AnyBaseToDecimal() {
9-
}
7+
private static final int CHAR_OFFSET_FOR_DIGIT = '0';
8+
private static final int CHAR_OFFSET_FOR_UPPERCASE = 'A' - 10;
109

11-
public static void main(String[] args) {
12-
assert convertToDecimal("1010", 2) == Integer.valueOf("1010", 2);
13-
assert convertToDecimal("777", 8) == Integer.valueOf("777", 8);
14-
assert convertToDecimal("999", 10) == Integer.valueOf("999", 10);
15-
assert convertToDecimal("ABCDEF", 16) == Integer.valueOf("ABCDEF", 16);
16-
assert convertToDecimal("XYZ", 36) == Integer.valueOf("XYZ", 36);
10+
private AnyBaseToDecimal() {
1711
}
1812

1913
/**
20-
* Convert any radix to decimal number
14+
* Convert any radix to a decimal number.
2115
*
22-
* @param s the string to be convert
23-
* @param radix the radix
24-
* @return decimal of bits
25-
* @throws NumberFormatException if {@code bits} or {@code radix} is invalid
16+
* @param input the string to be converted
17+
* @param radix the radix (base) of the input string
18+
* @return the decimal equivalent of the input string
19+
* @throws NumberFormatException if the input string or radix is invalid
2620
*/
27-
public static int convertToDecimal(String s, int radix) {
28-
int num = 0;
29-
int pow = 1;
21+
public static int convertToDecimal(String input, int radix) {
22+
int result = 0;
23+
int power = 1;
3024

31-
for (int i = s.length() - 1; i >= 0; i--) {
32-
int digit = valOfChar(s.charAt(i));
25+
for (int i = input.length() - 1; i >= 0; i--) {
26+
int digit = valOfChar(input.charAt(i));
3327
if (digit >= radix) {
34-
throw new NumberFormatException("For input string " + s);
28+
throw new NumberFormatException("For input string: " + input);
3529
}
36-
num += valOfChar(s.charAt(i)) * pow;
37-
pow *= radix;
30+
result += digit * power;
31+
power *= radix;
3832
}
39-
return num;
33+
return result;
4034
}
4135

4236
/**
43-
* Convert character to integer
37+
* Convert a character to its integer value.
4438
*
45-
* @param c the character
46-
* @return represented digit of given character
47-
* @throws NumberFormatException if {@code ch} is not UpperCase or Digit
48-
* character.
39+
* @param character the character to be converted
40+
* @return the integer value represented by the character
41+
* @throws NumberFormatException if the character is not an uppercase letter or a digit
4942
*/
50-
public static int valOfChar(char c) {
51-
if (!(Character.isUpperCase(c) || Character.isDigit(c))) {
52-
throw new NumberFormatException("invalid character :" + c);
43+
private static int valOfChar(char character) {
44+
if (Character.isDigit(character)) {
45+
return character - CHAR_OFFSET_FOR_DIGIT;
46+
} else if (Character.isUpperCase(character)) {
47+
return character - CHAR_OFFSET_FOR_UPPERCASE;
48+
} else {
49+
throw new NumberFormatException("invalid character:" + character);
5350
}
54-
return Character.isDigit(c) ? c - '0' : c - 'A' + 10;
5551
}
5652
}
Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
64
* This class converts a Binary number to a Decimal number
75
*/
86
final class BinaryToDecimal {
9-
private BinaryToDecimal() {
10-
}
7+
private static final int BINARY_BASE = 2;
118

12-
public static long binaryToDecimal(long binNum) {
13-
long binCopy;
14-
long d;
15-
long s = 0;
16-
long power = 0;
17-
binCopy = binNum;
18-
while (binCopy != 0) {
19-
d = binCopy % 10;
20-
s += d * (long) Math.pow(2, power++);
21-
binCopy /= 10;
22-
}
23-
return s;
9+
private BinaryToDecimal() {
2410
}
2511

2612
/**
27-
* Main Method
13+
* Converts a binary number to its decimal equivalent.
2814
*
29-
* @param args Command line arguments
15+
* @param binaryNumber The binary number to convert.
16+
* @return The decimal equivalent of the binary number.
17+
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
3018
*/
31-
public static void main(String[] args) {
32-
Scanner sc = new Scanner(System.in);
33-
System.out.print("Binary number: ");
34-
System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextLong()));
35-
sc.close();
19+
public static long binaryToDecimal(long binaryNumber) {
20+
long decimalValue = 0;
21+
long power = 0;
22+
23+
while (binaryNumber != 0) {
24+
long digit = binaryNumber % 10;
25+
if (digit > 1) {
26+
throw new IllegalArgumentException("Incorrect binary digit: " + digit);
27+
}
28+
decimalValue += (long) (digit * Math.pow(BINARY_BASE, power++));
29+
binaryNumber /= 10;
30+
}
31+
return decimalValue;
3632
}
3733
}
Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,63 @@
11
package com.thealgorithms.conversions;
22

33
import java.util.HashMap;
4-
import java.util.Scanner;
4+
import java.util.Map;
55

66
/**
77
* Converts any Binary Number to a Hexadecimal Number
88
*
99
* @author Nishita Aggarwal
1010
*/
1111
public final class BinaryToHexadecimal {
12+
private static final int BITS_IN_HEX_DIGIT = 4;
13+
private static final int BASE_BINARY = 2;
14+
private static final int BASE_DECIMAL = 10;
15+
private static final int HEX_START_DECIMAL = 10;
16+
private static final int HEX_END_DECIMAL = 15;
17+
1218
private BinaryToHexadecimal() {
1319
}
1420

1521
/**
16-
* This method converts a binary number to a hexadecimal number.
22+
* Converts a binary number to a hexadecimal number.
1723
*
18-
* @param binary The binary number
19-
* @return The hexadecimal number
24+
* @param binary The binary number to convert.
25+
* @return The hexadecimal representation of the binary number.
26+
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
2027
*/
21-
static String binToHex(int binary) {
22-
// hm to store hexadecimal codes for binary numbers within the range: 0000 to 1111 i.e. for
23-
// decimal numbers 0 to 15
24-
HashMap<Integer, String> hm = new HashMap<>();
25-
// String to store hexadecimal code
26-
String hex = "";
27-
int i;
28-
for (i = 0; i < 10; i++) {
29-
hm.put(i, String.valueOf(i));
30-
}
31-
for (i = 10; i < 16; i++) {
32-
hm.put(i, String.valueOf((char) ('A' + i - 10)));
33-
}
34-
int currbit;
28+
public static String binToHex(int binary) {
29+
Map<Integer, String> hexMap = initializeHexMap();
30+
StringBuilder hex = new StringBuilder();
31+
3532
while (binary != 0) {
36-
int code4 = 0; // to store decimal equivalent of number formed by 4 decimal digits
37-
for (i = 0; i < 4; i++) {
38-
currbit = binary % 10;
39-
binary = binary / 10;
40-
code4 += currbit * (int) Math.pow(2, i);
33+
int decimalValue = 0;
34+
for (int i = 0; i < BITS_IN_HEX_DIGIT; i++) {
35+
int currentBit = binary % BASE_DECIMAL;
36+
if (currentBit > 1) {
37+
throw new IllegalArgumentException("Incorrect binary digit: " + currentBit);
38+
}
39+
binary /= BASE_DECIMAL;
40+
decimalValue += (int) (currentBit * Math.pow(BASE_BINARY, i));
4141
}
42-
hex = hm.get(code4) + hex;
42+
hex.insert(0, hexMap.get(decimalValue));
4343
}
44-
return hex;
44+
45+
return !hex.isEmpty() ? hex.toString() : "0";
4546
}
4647

4748
/**
48-
* Main method
49+
* Initializes the hexadecimal map with decimal to hexadecimal mappings.
4950
*
50-
* @param args Command line arguments
51+
* @return The initialized map containing mappings from decimal numbers to hexadecimal digits.
5152
*/
52-
public static void main(String[] args) {
53-
Scanner sc = new Scanner(System.in);
54-
System.out.println("Enter binary number:");
55-
int binary = sc.nextInt();
56-
String hex = binToHex(binary);
57-
System.out.println("Hexadecimal Code:" + hex);
58-
sc.close();
53+
private static Map<Integer, String> initializeHexMap() {
54+
Map<Integer, String> hexMap = new HashMap<>();
55+
for (int i = 0; i < BASE_DECIMAL; i++) {
56+
hexMap.put(i, String.valueOf(i));
57+
}
58+
for (int i = HEX_START_DECIMAL; i <= HEX_END_DECIMAL; i++) {
59+
hexMap.put(i, String.valueOf((char) ('A' + i - HEX_START_DECIMAL)));
60+
}
61+
return hexMap;
5962
}
6063
}

0 commit comments

Comments
 (0)