Skip to content

Commit a11d512

Browse files
authored
Merge branch 'master' into ip_converter_new_algo
2 parents bed2d6a + e291516 commit a11d512

File tree

11 files changed

+607
-18
lines changed

11 files changed

+607
-18
lines changed

DIRECTORY.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [WordPatternMatcher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordPatternMatcher.java)
2424
* [WordSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordSearch.java)
2525
* bitmanipulation
26+
* [BcdConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java)
2627
* [BinaryPalindromeCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java)
2728
* [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java)
2829
* [BooleanAlgebraGates](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java)
@@ -48,6 +49,7 @@
4849
* [SingleElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleElement.java)
4950
* [SwapAdjacentBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java)
5051
* [TwosComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java)
52+
* [Xs3Conversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/Xs3Conversion.java)
5153
* ciphers
5254
* a5
5355
* [A5Cipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/A5Cipher.java)
@@ -597,7 +599,9 @@
597599
* [MaximumMinimumWindow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MaximumMinimumWindow.java)
598600
* [NextGreaterElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextGreaterElement.java)
599601
* [NextSmallerElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java)
602+
* [PostfixEvaluator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixEvaluator.java)
600603
* [PostfixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixToInfix.java)
604+
* [PrefixEvaluator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixEvaluator.java)
601605
* [PrefixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixToInfix.java)
602606
* [SortStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/SortStack.java)
603607
* [StackPostfixNotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java)
@@ -660,6 +664,7 @@
660664
* [WordPatternMatcherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordPatternMatcherTest.java)
661665
* [WordSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordSearchTest.java)
662666
* bitmanipulation
667+
* [BcdConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java)
663668
* [BinaryPalindromeCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java)
664669
* [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java)
665670
* [BooleanAlgebraGatesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGatesTest.java)
@@ -685,6 +690,7 @@
685690
* [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java)
686691
* [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java)
687692
* [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java)
693+
* [Xs3ConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/Xs3ConversionTest.java)
688694
* ciphers
689695
* a5
690696
* [A5CipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5CipherTest.java)
@@ -1022,6 +1028,7 @@
10221028
* [SkylineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SkylineProblemTest.java)
10231029
* [SudokuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SudokuTest.java)
10241030
* [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java)
1031+
* [TowerOfHanoiTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java)
10251032
* [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java)
10261033
* [WorstFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/WorstFitCPUTest.java)
10271034
* Recursion
@@ -1125,7 +1132,9 @@
11251132
* [LargestRectangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/LargestRectangleTest.java)
11261133
* [NextGreaterElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextGreaterElementTest.java)
11271134
* [NextSmallerElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextSmallerElementTest.java)
1135+
* [PostfixEvaluatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java)
11281136
* [PostfixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixToInfixTest.java)
1137+
* [PrefixEvaluatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixEvaluatorTest.java)
11291138
* [PrefixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixToInfixTest.java)
11301139
* [SortStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/SortStackTest.java)
11311140
* [StackPostfixNotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/StackPostfixNotationTest.java)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides methods to convert between BCD (Binary-Coded Decimal) and decimal numbers.
5+
*
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.
7+
*
8+
* For more information, refer to the
9+
* <a href="https://en.wikipedia.org/wiki/Binary-coded_decimal">Binary-Coded Decimal</a> Wikipedia page.
10+
*
11+
* <b>Example usage:</b>
12+
* <pre>
13+
* int decimal = BcdConversion.bcdToDecimal(0x1234);
14+
* System.out.println("BCD 0x1234 to decimal: " + decimal); // Output: 1234
15+
*
16+
* int bcd = BcdConversion.decimalToBcd(1234);
17+
* System.out.println("Decimal 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
18+
* </pre>
19+
*/
20+
public final class BcdConversion {
21+
private BcdConversion() {
22+
}
23+
24+
/**
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.
32+
*
33+
* @param bcd The BCD number.
34+
* @return The corresponding decimal number.
35+
* @throws IllegalArgumentException if the BCD number contains invalid digits.
36+
*/
37+
public static int bcdToDecimal(int bcd) {
38+
int decimal = 0;
39+
int multiplier = 1;
40+
41+
// Validate BCD digits
42+
while (bcd > 0) {
43+
int digit = bcd & 0xF;
44+
if (digit > 9) {
45+
throw new IllegalArgumentException("Invalid BCD digit: " + digit);
46+
}
47+
decimal += digit * multiplier;
48+
multiplier *= 10;
49+
bcd >>= 4;
50+
}
51+
return decimal;
52+
}
53+
54+
/**
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.
62+
*
63+
* @param decimal The decimal number.
64+
* @return The corresponding BCD number.
65+
* @throws IllegalArgumentException if the decimal number is greater than 9999.
66+
*/
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+
72+
int bcd = 0;
73+
int shift = 0;
74+
while (decimal > 0) {
75+
int digit = decimal % 10;
76+
bcd |= (digit << (shift * 4));
77+
decimal /= 10;
78+
shift++;
79+
}
80+
return bcd;
81+
}
82+
}
Lines changed: 58 additions & 0 deletions
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+
}
Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,65 @@
11
package com.thealgorithms.others;
22

3-
import java.util.Scanner;
3+
import java.util.List;
44

5+
/**
6+
* The {@code TowerOfHanoi} class provides a recursive solution to the Tower of Hanoi puzzle.
7+
* This puzzle involves moving a set of discs from one pole to another, following specific rules:
8+
* 1. Only one disc can be moved at a time.
9+
* 2. A disc can only be placed on top of a larger disc.
10+
* 3. All discs must start on one pole and end on another.
11+
*
12+
* This implementation recursively calculates the steps required to solve the puzzle and stores them
13+
* in a provided list.
14+
*
15+
* <p>
16+
* For more information about the Tower of Hanoi, see
17+
* <a href="https://en.wikipedia.org/wiki/Tower_of_Hanoi">Tower of Hanoi on Wikipedia</a>.
18+
* </p>
19+
*
20+
* The {@code shift} method takes the number of discs and the names of the poles,
21+
* and appends the steps required to solve the puzzle to the provided list.
22+
* Time Complexity: O(2^n) - Exponential time complexity due to the recursive nature of the problem.
23+
* Space Complexity: O(n) - Linear space complexity due to the recursion stack.
24+
* Wikipedia: https://en.wikipedia.org/wiki/Tower_of_Hanoi
25+
*/
526
final class TowerOfHanoi {
27+
628
private TowerOfHanoi() {
729
}
830

9-
public static void shift(int n, String startPole, String intermediatePole, String endPole) {
10-
// if n becomes zero the program returns thus ending the loop.
31+
/**
32+
* Recursively solve the Tower of Hanoi puzzle by moving discs between poles.
33+
*
34+
* @param n The number of discs to move.
35+
* @param startPole The name of the start pole from which discs are moved.
36+
* @param intermediatePole The name of the intermediate pole used as a temporary holding area.
37+
* @param endPole The name of the end pole to which discs are moved.
38+
* @param result A list to store the steps required to solve the puzzle.
39+
*
40+
* <p>
41+
* This method is called recursively to move n-1 discs
42+
* to the intermediate pole,
43+
* then moves the nth disc to the end pole, and finally
44+
* moves the n-1 discs from the
45+
* intermediate pole to the end pole.
46+
* </p>
47+
*
48+
* <p>
49+
* Time Complexity: O(2^n) - Exponential time complexity due to the recursive nature of the problem.
50+
* Space Complexity: O(n) - Linear space complexity due to the recursion stack.
51+
* </p>
52+
*/
53+
public static void shift(int n, String startPole, String intermediatePole, String endPole, List<String> result) {
1154
if (n != 0) {
12-
// Shift function is called in recursion for swapping the n-1 disc from the startPole to
13-
// the intermediatePole
14-
shift(n - 1, startPole, endPole, intermediatePole);
15-
System.out.format("Move %d from %s to %s%n", n, startPole, endPole); // Result Printing
16-
// Shift function is called in recursion for swapping the n-1 disc from the
17-
// intermediatePole to the endPole
18-
shift(n - 1, intermediatePole, startPole, endPole);
19-
}
20-
}
55+
// Move n-1 discs from startPole to intermediatePole
56+
shift(n - 1, startPole, endPole, intermediatePole, result);
2157

22-
public static void main(String[] args) {
23-
System.out.print("Enter number of discs on Pole 1: ");
24-
Scanner scanner = new Scanner(System.in);
25-
int numberOfDiscs = scanner.nextInt(); // input of number of discs on pole 1
26-
shift(numberOfDiscs, "Pole1", "Pole2", "Pole3"); // Shift function called
27-
scanner.close();
58+
// Add the move of the nth disc from startPole to endPole
59+
result.add(String.format("Move %d from %s to %s", n, startPole, endPole));
60+
61+
// Move the n-1 discs from intermediatePole to endPole
62+
shift(n - 1, intermediatePole, startPole, endPole, result);
63+
}
2864
}
2965
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Set;
4+
import java.util.Stack;
5+
6+
/**
7+
* Evaluate a postfix (Reverse Polish) expression using a stack.
8+
*
9+
* <p>Example: Expression "5 6 + 2 *" results in 22.
10+
* <p>Applications: Used in calculators and expression evaluation in compilers.
11+
*
12+
* @author Hardvan
13+
*/
14+
public final class PostfixEvaluator {
15+
private PostfixEvaluator() {
16+
}
17+
18+
private static final Set<String> OPERATORS = Set.of("+", "-", "*", "/");
19+
20+
/**
21+
* Evaluates the given postfix expression and returns the result.
22+
*
23+
* @param expression The postfix expression as a string with operands and operators separated by spaces.
24+
* @return The result of evaluating the postfix expression.
25+
* @throws IllegalArgumentException if the expression is invalid.
26+
*/
27+
public static int evaluatePostfix(String expression) {
28+
Stack<Integer> stack = new Stack<>();
29+
30+
for (String token : expression.split("\\s+")) {
31+
if (isOperator(token)) {
32+
int operand2 = stack.pop();
33+
int operand1 = stack.pop();
34+
stack.push(applyOperator(token, operand1, operand2));
35+
} else {
36+
stack.push(Integer.valueOf(token));
37+
}
38+
}
39+
40+
if (stack.size() != 1) {
41+
throw new IllegalArgumentException("Invalid expression");
42+
}
43+
44+
return stack.pop();
45+
}
46+
47+
/**
48+
* Checks if the given token is an operator.
49+
*
50+
* @param token The token to check.
51+
* @return true if the token is an operator, false otherwise.
52+
*/
53+
private static boolean isOperator(String token) {
54+
return OPERATORS.contains(token);
55+
}
56+
57+
/**
58+
* Applies the given operator to the two operands.
59+
*
60+
* @param operator The operator to apply.
61+
* @param a The first operand.
62+
* @param b The second operand.
63+
* @return The result of applying the operator to the operands.
64+
*/
65+
private static int applyOperator(String operator, int a, int b) {
66+
return switch (operator) {
67+
case "+" -> a + b;
68+
case "-" -> a - b;
69+
case "*" -> a * b;
70+
case "/" -> a / b;
71+
default -> throw new IllegalArgumentException("Invalid operator");
72+
};
73+
}
74+
}

0 commit comments

Comments
 (0)