Skip to content

Commit 60a7169

Browse files
authored
Merge branch 'master' into refactor_bcd
2 parents 390b83d + bae7f89 commit 60a7169

File tree

9 files changed

+438
-18
lines changed

9 files changed

+438
-18
lines changed

DIRECTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* [SingleElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleElement.java)
5050
* [SwapAdjacentBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java)
5151
* [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)
5253
* ciphers
5354
* a5
5455
* [A5Cipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/A5Cipher.java)
@@ -597,7 +598,9 @@
597598
* [MaximumMinimumWindow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MaximumMinimumWindow.java)
598599
* [NextGreaterElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextGreaterElement.java)
599600
* [NextSmallerElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java)
601+
* [PostfixEvaluator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixEvaluator.java)
600602
* [PostfixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixToInfix.java)
603+
* [PrefixEvaluator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixEvaluator.java)
601604
* [PrefixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixToInfix.java)
602605
* [SortStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/SortStack.java)
603606
* [StackPostfixNotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java)
@@ -686,6 +689,7 @@
686689
* [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java)
687690
* [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java)
688691
* [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java)
692+
* [Xs3ConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/Xs3ConversionTest.java)
689693
* ciphers
690694
* a5
691695
* [A5CipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5CipherTest.java)
@@ -1022,6 +1026,7 @@
10221026
* [SkylineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SkylineProblemTest.java)
10231027
* [SudokuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SudokuTest.java)
10241028
* [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java)
1029+
* [TowerOfHanoiTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java)
10251030
* [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java)
10261031
* [WorstFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/WorstFitCPUTest.java)
10271032
* Recursion
@@ -1125,7 +1130,9 @@
11251130
* [LargestRectangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/LargestRectangleTest.java)
11261131
* [NextGreaterElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextGreaterElementTest.java)
11271132
* [NextSmallerElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextSmallerElementTest.java)
1133+
* [PostfixEvaluatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java)
11281134
* [PostfixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixToInfixTest.java)
1135+
* [PrefixEvaluatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixEvaluatorTest.java)
11291136
* [PrefixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixToInfixTest.java)
11301137
* [SortStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/SortStackTest.java)
11311138
* [StackPostfixNotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/StackPostfixNotationTest.java)
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+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Set;
4+
import java.util.Stack;
5+
6+
/**
7+
* Evaluate a prefix (Polish) expression using a stack.
8+
*
9+
* <p>Example: Expression "+ * 2 3 4" results in 10.
10+
* <p>Applications: Useful for implementing compilers and interpreters.
11+
*
12+
* @author Hardvan
13+
*/
14+
public final class PrefixEvaluator {
15+
private PrefixEvaluator() {
16+
}
17+
18+
private static final Set<String> OPERATORS = Set.of("+", "-", "*", "/");
19+
20+
/**
21+
* Evaluates the given prefix expression and returns the result.
22+
*
23+
* @param expression The prefix expression as a string with operands and operators separated by spaces.
24+
* @return The result of evaluating the prefix expression.
25+
* @throws IllegalArgumentException if the expression is invalid.
26+
*/
27+
public static int evaluatePrefix(String expression) {
28+
Stack<Integer> stack = new Stack<>();
29+
String[] tokens = expression.split("\\s+");
30+
31+
for (int i = tokens.length - 1; i >= 0; i--) {
32+
String token = tokens[i];
33+
if (isOperator(token)) {
34+
int operand1 = stack.pop();
35+
int operand2 = stack.pop();
36+
stack.push(applyOperator(token, operand1, operand2));
37+
} else {
38+
stack.push(Integer.valueOf(token));
39+
}
40+
}
41+
42+
if (stack.size() != 1) {
43+
throw new IllegalArgumentException("Invalid expression");
44+
}
45+
46+
return stack.pop();
47+
}
48+
49+
/**
50+
* Checks if the given token is an operator.
51+
*
52+
* @param token The token to check.
53+
* @return true if the token is an operator, false otherwise.
54+
*/
55+
private static boolean isOperator(String token) {
56+
return OPERATORS.contains(token);
57+
}
58+
59+
/**
60+
* Applies the given operator to the two operands.
61+
*
62+
* @param operator The operator to apply.
63+
* @param a The first operand.
64+
* @param b The second operand.
65+
* @return The result of applying the operator to the operands.
66+
*/
67+
private static int applyOperator(String operator, int a, int b) {
68+
return switch (operator) {
69+
case "+" -> a + b;
70+
case "-" -> a - b;
71+
case "*" -> a * b;
72+
case "/" -> a / b;
73+
default -> throw new IllegalArgumentException("Invalid operator");
74+
};
75+
}
76+
}

0 commit comments

Comments
 (0)