Skip to content

Commit 1a321e1

Browse files
authored
Merge branch 'master' into evaluate_prefix_new_algo
2 parents 6f50206 + b0c8a8f commit 1a321e1

File tree

7 files changed

+333
-18
lines changed

7 files changed

+333
-18
lines changed

DIRECTORY.md

Lines changed: 5 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,6 +598,7 @@
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)
601603
* [PrefixEvaluator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixEvaluator.java)
602604
* [PrefixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixToInfix.java)
@@ -687,6 +689,7 @@
687689
* [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java)
688690
* [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java)
689691
* [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)
690693
* ciphers
691694
* a5
692695
* [A5CipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5CipherTest.java)
@@ -1023,6 +1026,7 @@
10231026
* [SkylineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SkylineProblemTest.java)
10241027
* [SudokuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SudokuTest.java)
10251028
* [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)
10261030
* [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java)
10271031
* [WorstFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/WorstFitCPUTest.java)
10281032
* Recursion
@@ -1126,6 +1130,7 @@
11261130
* [LargestRectangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/LargestRectangleTest.java)
11271131
* [NextGreaterElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextGreaterElementTest.java)
11281132
* [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)
11291134
* [PostfixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixToInfixTest.java)
11301135
* [PrefixEvaluatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixEvaluatorTest.java)
11311136
* [PrefixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixToInfixTest.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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Unit tests for the Xs3Conversion class.
9+
*/
10+
public class Xs3ConversionTest {
11+
12+
/**
13+
* Test the xs3ToBinary method with an XS-3 number.
14+
*/
15+
@Test
16+
public void testXs3ToBinary() {
17+
int binary = Xs3Conversion.xs3ToBinary(0x4567);
18+
assertEquals(1234, binary); // XS-3 0x4567 should convert to binary 1234
19+
}
20+
21+
/**
22+
* Test the binaryToXs3 method with a binary number.
23+
*/
24+
@Test
25+
public void testBinaryToXs3() {
26+
int xs3 = Xs3Conversion.binaryToXs3(1234);
27+
assertEquals(0x4567, xs3); // Binary 1234 should convert to XS-3 0x4567
28+
}
29+
30+
/**
31+
* Test the xs3ToBinary method with zero.
32+
*/
33+
@Test
34+
public void testXs3ToBinaryZero() {
35+
int binary = Xs3Conversion.xs3ToBinary(0x0);
36+
assertEquals(0, binary); // XS-3 0x0 should convert to binary 0
37+
}
38+
39+
/**
40+
* Test the binaryToXs3 method with zero.
41+
*/
42+
@Test
43+
public void testBinaryToXs3Zero() {
44+
int xs3 = Xs3Conversion.binaryToXs3(0);
45+
assertEquals(0x0, xs3); // Binary 0 should convert to XS-3 0x0
46+
}
47+
48+
/**
49+
* Test the xs3ToBinary method with a single digit XS-3 number.
50+
*/
51+
@Test
52+
public void testXs3ToBinarySingleDigit() {
53+
int binary = Xs3Conversion.xs3ToBinary(0x5);
54+
assertEquals(2, binary); // XS-3 0x5 should convert to binary 2
55+
}
56+
57+
/**
58+
* Test the binaryToXs3 method with a single digit binary number.
59+
*/
60+
@Test
61+
public void testBinaryToXs3SingleDigit() {
62+
int xs3 = Xs3Conversion.binaryToXs3(2);
63+
assertEquals(0x5, xs3); // Binary 2 should convert to XS-3 0x5
64+
}
65+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class TowerOfHanoiTest {
11+
12+
@Test
13+
public void testHanoiWithOneDisc() {
14+
List<String> result = new ArrayList<>();
15+
TowerOfHanoi.shift(1, "Pole1", "Pole2", "Pole3", result);
16+
17+
// Expected output for 1 disc
18+
List<String> expected = List.of("Move 1 from Pole1 to Pole3");
19+
assertEquals(expected, result);
20+
}
21+
22+
@Test
23+
public void testHanoiWithTwoDiscs() {
24+
List<String> result = new ArrayList<>();
25+
TowerOfHanoi.shift(2, "Pole1", "Pole2", "Pole3", result);
26+
27+
// Expected output for 2 discs
28+
List<String> expected = List.of("Move 1 from Pole1 to Pole2", "Move 2 from Pole1 to Pole3", "Move 1 from Pole2 to Pole3");
29+
assertEquals(expected, result);
30+
}
31+
32+
@Test
33+
public void testHanoiWithThreeDiscs() {
34+
List<String> result = new ArrayList<>();
35+
TowerOfHanoi.shift(3, "Pole1", "Pole2", "Pole3", result);
36+
37+
// Expected output for 3 discs
38+
List<String> expected = List.of("Move 1 from Pole1 to Pole3", "Move 2 from Pole1 to Pole2", "Move 1 from Pole3 to Pole2", "Move 3 from Pole1 to Pole3", "Move 1 from Pole2 to Pole1", "Move 2 from Pole2 to Pole3", "Move 1 from Pole1 to Pole3");
39+
assertEquals(expected, result);
40+
}
41+
42+
@Test
43+
public void testHanoiWithZeroDiscs() {
44+
List<String> result = new ArrayList<>();
45+
TowerOfHanoi.shift(0, "Pole1", "Pole2", "Pole3", result);
46+
47+
// There should be no moves if there are 0 discs
48+
assertTrue(result.isEmpty());
49+
}
50+
}

0 commit comments

Comments
 (0)