Skip to content

Commit ef56720

Browse files
authored
Merge branch 'master' into minstack2_new_algo
2 parents f1a44f5 + 8886e09 commit ef56720

File tree

85 files changed

+2780
-330
lines changed

Some content is hidden

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

85 files changed

+2780
-330
lines changed

DIRECTORY.md

+43
Large diffs are not rendered by default.

checkstyle.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
<!-- See https://checkstyle.org/checks/misc/index.html -->
184184
<module name="ArrayTypeStyle"/>
185185
<!-- TODO <module name="FinalParameters"/> -->
186-
<!-- TODO <module name="TodoComment"/> -->
186+
<module name="TodoComment"/>
187187
<module name="UpperEll"/>
188188

189189
<!-- https://checkstyle.org/filters/suppressionxpathfilter.html -->

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
<version>${assertj.version}</version>
4141
<scope>test</scope>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.mockito</groupId>
45+
<artifactId>mockito-core</artifactId>
46+
<version>5.14.1</version>
47+
<scope>test</scope>
48+
</dependency>
49+
4350

4451
<dependency>
4552
<groupId>org.junit.jupiter</groupId>

spotbugs-exclude.xml

-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@
8787
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" />
8888
</Match>
8989
<!-- fb-contrib -->
90-
<Match>
91-
<Bug pattern="OCP_OVERLY_CONCRETE_PARAMETER" />
92-
</Match>
9390
<Match>
9491
<Bug pattern="LSC_LITERAL_STRING_COMPARISON" />
9592
</Match>

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.backtracking;
22

33
import java.util.ArrayList;
4+
import java.util.Collection;
45
import java.util.List;
56

67
/**
@@ -95,7 +96,7 @@ public static void removeWord(char[][] puzzle, String word, int row, int col, bo
9596
* @param words The list of words to be placed.
9697
* @return true if the crossword is solved, false otherwise.
9798
*/
98-
public static boolean solveCrossword(char[][] puzzle, List<String> words) {
99+
public static boolean solveCrossword(char[][] puzzle, Collection<String> words) {
99100
// Create a mutable copy of the words list
100101
List<String> remainingWords = new ArrayList<>(words);
101102

src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java

+45-21
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,80 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* This class provides methods to convert between BCD (Binary-Coded Decimal) and binary.
4+
* This class provides methods to convert between BCD (Binary-Coded Decimal) and decimal numbers.
55
*
6-
* Binary-Coded Decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of binary digits, usually four or eight.
6+
* BCD is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of binary digits, usually four or eight.
77
*
88
* For more information, refer to the
99
* <a href="https://en.wikipedia.org/wiki/Binary-coded_decimal">Binary-Coded Decimal</a> Wikipedia page.
1010
*
1111
* <b>Example usage:</b>
1212
* <pre>
13-
* int binary = BcdConversion.bcdToBinary(0x1234);
14-
* System.out.println("BCD 0x1234 to binary: " + binary); // Output: 1234
13+
* int decimal = BcdConversion.bcdToDecimal(0x1234);
14+
* System.out.println("BCD 0x1234 to decimal: " + decimal); // Output: 1234
1515
*
16-
* int bcd = BcdConversion.binaryToBcd(1234);
17-
* System.out.println("Binary 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
16+
* int bcd = BcdConversion.decimalToBcd(1234);
17+
* System.out.println("Decimal 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
1818
* </pre>
1919
*/
2020
public final class BcdConversion {
2121
private BcdConversion() {
2222
}
23+
2324
/**
24-
* Converts a BCD (Binary-Coded Decimal) number to binary.
25+
* Converts a BCD (Binary-Coded Decimal) number to a decimal number.
26+
* <p>Steps:
27+
* <p>1. Validate the BCD number to ensure all digits are between 0 and 9.
28+
* <p>2. Extract the last 4 bits (one BCD digit) from the BCD number.
29+
* <p>3. Multiply the extracted digit by the corresponding power of 10 and add it to the decimal number.
30+
* <p>4. Shift the BCD number right by 4 bits to process the next BCD digit.
31+
* <p>5. Repeat steps 1-4 until the BCD number is zero.
2532
*
2633
* @param bcd The BCD number.
27-
* @return The corresponding binary number.
34+
* @return The corresponding decimal number.
35+
* @throws IllegalArgumentException if the BCD number contains invalid digits.
2836
*/
29-
public static int bcdToBinary(int bcd) {
30-
int binary = 0;
37+
public static int bcdToDecimal(int bcd) {
38+
int decimal = 0;
3139
int multiplier = 1;
40+
41+
// Validate BCD digits
3242
while (bcd > 0) {
33-
int digit = bcd & 0xF; // Extract the last 4 bits (one BCD digit)
34-
binary += digit * multiplier;
43+
int digit = bcd & 0xF;
44+
if (digit > 9) {
45+
throw new IllegalArgumentException("Invalid BCD digit: " + digit);
46+
}
47+
decimal += digit * multiplier;
3548
multiplier *= 10;
36-
bcd >>= 4; // Shift right by 4 bits to process the next BCD digit
49+
bcd >>= 4;
3750
}
38-
return binary;
51+
return decimal;
3952
}
4053

4154
/**
42-
* Converts a binary number to BCD (Binary-Coded Decimal).
55+
* Converts a decimal number to BCD (Binary-Coded Decimal).
56+
* <p>Steps:
57+
* <p>1. Check if the decimal number is within the valid range for BCD (0 to 9999).
58+
* <p>2. Extract the last decimal digit from the decimal number.
59+
* <p>3. Shift the digit to the correct BCD position and add it to the BCD number.
60+
* <p>4. Remove the last decimal digit from the decimal number.
61+
* <p>5. Repeat steps 2-4 until the decimal number is zero.
4362
*
44-
* @param binary The binary number.
63+
* @param decimal The decimal number.
4564
* @return The corresponding BCD number.
65+
* @throws IllegalArgumentException if the decimal number is greater than 9999.
4666
*/
47-
public static int binaryToBcd(int binary) {
67+
public static int decimalToBcd(int decimal) {
68+
if (decimal < 0 || decimal > 9999) {
69+
throw new IllegalArgumentException("Value out of bounds for BCD representation: " + decimal);
70+
}
71+
4872
int bcd = 0;
4973
int shift = 0;
50-
while (binary > 0) {
51-
int digit = binary % 10; // Extract the last decimal digit
52-
bcd |= (digit << (shift * 4)); // Shift the digit to the correct BCD position
53-
binary /= 10; // Remove the last decimal digit
74+
while (decimal > 0) {
75+
int digit = decimal % 10;
76+
bcd |= (digit << (shift * 4));
77+
decimal /= 10;
5478
shift++;
5579
}
5680
return bcd;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides methods to convert between XS-3 (Excess-3) and binary.
5+
*
6+
* Excess-3, also called XS-3, is a binary-coded decimal (BCD) code in which each decimal digit is represented by its corresponding 4-bit binary value plus 3.
7+
*
8+
* For more information, refer to the
9+
* <a href="https://en.wikipedia.org/wiki/Excess-3">Excess-3</a> Wikipedia page.
10+
*
11+
* <b>Example usage:</b>
12+
* <pre>
13+
* int binary = Xs3Conversion.xs3ToBinary(0x4567);
14+
* System.out.println("XS-3 0x4567 to binary: " + binary); // Output: 1234
15+
*
16+
* int xs3 = Xs3Conversion.binaryToXs3(1234);
17+
* System.out.println("Binary 1234 to XS-3: " + Integer.toHexString(xs3)); // Output: 0x4567
18+
* </pre>
19+
*/
20+
public final class Xs3Conversion {
21+
private Xs3Conversion() {
22+
}
23+
/**
24+
* Converts an XS-3 (Excess-3) number to binary.
25+
*
26+
* @param xs3 The XS-3 number.
27+
* @return The corresponding binary number.
28+
*/
29+
public static int xs3ToBinary(int xs3) {
30+
int binary = 0;
31+
int multiplier = 1;
32+
while (xs3 > 0) {
33+
int digit = (xs3 & 0xF) - 3; // Extract the last 4 bits (one XS-3 digit) and subtract 3
34+
binary += digit * multiplier;
35+
multiplier *= 10;
36+
xs3 >>= 4; // Shift right by 4 bits to process the next XS-3 digit
37+
}
38+
return binary;
39+
}
40+
41+
/**
42+
* Converts a binary number to XS-3 (Excess-3).
43+
*
44+
* @param binary The binary number.
45+
* @return The corresponding XS-3 number.
46+
*/
47+
public static int binaryToXs3(int binary) {
48+
int xs3 = 0;
49+
int shift = 0;
50+
while (binary > 0) {
51+
int digit = (binary % 10) + 3; // Extract the last decimal digit and add 3
52+
xs3 |= (digit << (shift * 4)); // Shift the digit to the correct XS-3 position
53+
binary /= 10; // Remove the last decimal digit
54+
shift++;
55+
}
56+
return xs3;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.conversions;
2+
3+
/**
4+
* Converts between big-endian and little-endian formats.
5+
* Big-endian is the most significant byte first, while little-endian is the least significant byte first.
6+
* Big-endian to little-endian: 0x12345678 -> 0x78563412
7+
*
8+
* Little-endian to big-endian: 0x12345678 -> 0x78563412
9+
*
10+
* @author Hardvan
11+
*/
12+
public final class EndianConverter {
13+
private EndianConverter() {
14+
}
15+
16+
public static int bigToLittleEndian(int value) {
17+
return Integer.reverseBytes(value);
18+
}
19+
20+
public static int littleToBigEndian(int value) {
21+
return Integer.reverseBytes(value);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.conversions;
2+
3+
/**
4+
* Converts an IPv4 address to its binary equivalent and vice-versa.
5+
* IP to Binary: Converts an IPv4 address to its binary equivalent.
6+
* Example: 127.3.4.5 -> 01111111.00000011.00000100.00000101
7+
*
8+
* Binary to IP: Converts a binary equivalent to an IPv4 address.
9+
* Example: 01111111.00000011.00000100.00000101 -> 127.3.4.5
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class IPConverter {
14+
private IPConverter() {
15+
}
16+
17+
/**
18+
* Converts an IPv4 address to its binary equivalent.
19+
* @param ip The IPv4 address to convert.
20+
* @return The binary equivalent of the IPv4 address.
21+
*/
22+
public static String ipToBinary(String ip) {
23+
StringBuilder binary = new StringBuilder();
24+
for (String octet : ip.split("\\.")) {
25+
binary.append(octetToBinary(Integer.parseInt(octet))).append(".");
26+
}
27+
return binary.substring(0, binary.length() - 1);
28+
}
29+
30+
/**
31+
* Converts a single octet to its 8-bit binary representation.
32+
* @param octet The octet to convert (0-255).
33+
* @return The 8-bit binary representation as a String.
34+
*/
35+
private static String octetToBinary(int octet) {
36+
char[] binary = {'0', '0', '0', '0', '0', '0', '0', '0'};
37+
for (int i = 7; i >= 0; i--) {
38+
if ((octet & 1) == 1) {
39+
binary[i] = '1';
40+
}
41+
octet >>>= 1;
42+
}
43+
return new String(binary);
44+
}
45+
46+
/**
47+
* Converts a binary equivalent to an IPv4 address.
48+
* @param binary The binary equivalent to convert.
49+
* @return The IPv4 address of the binary equivalent.
50+
*/
51+
public static String binaryToIP(String binary) {
52+
StringBuilder ip = new StringBuilder();
53+
for (String octet : binary.split("\\.")) {
54+
ip.append(Integer.parseInt(octet, 2)).append(".");
55+
}
56+
return ip.substring(0, ip.length() - 1);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Converts text to Morse code and vice-versa.
8+
* Text to Morse code: Each letter is separated by a space and each word is separated by a pipe (|).
9+
* Example: "HELLO WORLD" -> ".... . .-.. .-.. --- | .-- --- .-. .-.. -.."
10+
*
11+
* Morse code to text: Each letter is separated by a space and each word is separated by a pipe (|).
12+
* Example: ".... . .-.. .-.. --- | .-- --- .-. .-.. -.." -> "HELLO WORLD"
13+
*
14+
* Applications: Used in radio communications and algorithmic challenges.
15+
*
16+
* @author Hardvan
17+
*/
18+
public final class MorseCodeConverter {
19+
private MorseCodeConverter() {
20+
}
21+
22+
private static final Map<Character, String> MORSE_MAP = new HashMap<>();
23+
private static final Map<String, Character> REVERSE_MAP = new HashMap<>();
24+
25+
static {
26+
MORSE_MAP.put('A', ".-");
27+
MORSE_MAP.put('B', "-...");
28+
MORSE_MAP.put('C', "-.-.");
29+
MORSE_MAP.put('D', "-..");
30+
MORSE_MAP.put('E', ".");
31+
MORSE_MAP.put('F', "..-.");
32+
MORSE_MAP.put('G', "--.");
33+
MORSE_MAP.put('H', "....");
34+
MORSE_MAP.put('I', "..");
35+
MORSE_MAP.put('J', ".---");
36+
MORSE_MAP.put('K', "-.-");
37+
MORSE_MAP.put('L', ".-..");
38+
MORSE_MAP.put('M', "--");
39+
MORSE_MAP.put('N', "-.");
40+
MORSE_MAP.put('O', "---");
41+
MORSE_MAP.put('P', ".--.");
42+
MORSE_MAP.put('Q', "--.-");
43+
MORSE_MAP.put('R', ".-.");
44+
MORSE_MAP.put('S', "...");
45+
MORSE_MAP.put('T', "-");
46+
MORSE_MAP.put('U', "..-");
47+
MORSE_MAP.put('V', "...-");
48+
MORSE_MAP.put('W', ".--");
49+
MORSE_MAP.put('X', "-..-");
50+
MORSE_MAP.put('Y', "-.--");
51+
MORSE_MAP.put('Z', "--..");
52+
53+
// Build reverse map for decoding
54+
MORSE_MAP.forEach((k, v) -> REVERSE_MAP.put(v, k));
55+
}
56+
57+
/**
58+
* Converts text to Morse code.
59+
* Each letter is separated by a space and each word is separated by a pipe (|).
60+
*
61+
* @param text The text to convert to Morse code.
62+
* @return The Morse code representation of the text.
63+
*/
64+
public static String textToMorse(String text) {
65+
StringBuilder morse = new StringBuilder();
66+
String[] words = text.toUpperCase().split(" ");
67+
for (int i = 0; i < words.length; i++) {
68+
for (char c : words[i].toCharArray()) {
69+
morse.append(MORSE_MAP.getOrDefault(c, "")).append(" ");
70+
}
71+
if (i < words.length - 1) {
72+
morse.append("| ");
73+
}
74+
}
75+
return morse.toString().trim();
76+
}
77+
78+
/**
79+
* Converts Morse code to text.
80+
* Each letter is separated by a space and each word is separated by a pipe (|).
81+
*
82+
* @param morse The Morse code to convert to text.
83+
* @return The text representation of the Morse code.
84+
*/
85+
public static String morseToText(String morse) {
86+
StringBuilder text = new StringBuilder();
87+
String[] words = morse.split(" \\| ");
88+
for (int i = 0; i < words.length; i++) {
89+
for (String code : words[i].split(" ")) {
90+
text.append(REVERSE_MAP.getOrDefault(code, '?'));
91+
}
92+
if (i < words.length - 1) {
93+
text.append(" ");
94+
}
95+
}
96+
return text.toString();
97+
}
98+
}

0 commit comments

Comments
 (0)