From 887ee0f892c5cba0a0f94eef33d8265bf653a950 Mon Sep 17 00:00:00 2001
From: Hardik Pawar
Date: Thu, 3 Oct 2024 10:19:23 +0530
Subject: [PATCH 01/13] Add function documentation, enhance comments in
TowerOfHanoi.java
---
.../thealgorithms/others/TowerOfHanoi.java | 46 ++++++++++++++++---
1 file changed, 40 insertions(+), 6 deletions(-)
diff --git a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
index 2216799b987a..c929ecd74dc9 100644
--- a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
+++ b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
@@ -2,23 +2,57 @@
import java.util.Scanner;
+/**
+ * The {@code TowerOfHanoi} class provides functionality to solve the Tower of
+ * Hanoi puzzle.
+ * It uses recursion to move discs between poles and prints the steps to solve
+ * the puzzle.
+ * The main function interacts with the user to get the number of discs and
+ * calls the recursive {@code shift} function to perform the moves.
+ */
final class TowerOfHanoi {
+
private TowerOfHanoi() {
}
+ /**
+ * Recursively solves the Tower of Hanoi puzzle by moving discs between poles.
+ *
+ * @param n The number of discs to move.
+ * @param startPole The name of the start pole.
+ * @param intermediatePole The name of the intermediate pole.
+ * @param endPole The name of the end pole.
+ *
+ *
+ * This method is called recursively to move n-1 discs
+ * to the intermediate pole,
+ * then moves the nth disc to the end pole, and finally
+ * moves the n-1 discs from the
+ * intermediate pole to the end pole.
+ *
+ */
public static void shift(int n, String startPole, String intermediatePole, String endPole) {
- // if n becomes zero the program returns thus ending the loop.
+ // If n becomes zero, the program returns, ending the recursion.
if (n != 0) {
- // Shift function is called in recursion for swapping the n-1 disc from the startPole to
- // the intermediatePole
+ // Recursively move n-1 discs from startPole to intermediatePole
shift(n - 1, startPole, endPole, intermediatePole);
- System.out.format("Move %d from %s to %s%n", n, startPole, endPole); // Result Printing
- // Shift function is called in recursion for swapping the n-1 disc from the
- // intermediatePole to the endPole
+
+ // Print the move of the nth disc from startPole to endPole
+ System.out.format("Move %d from %s to %s%n", n, startPole, endPole);
+
+ // Recursively move the n-1 discs from intermediatePole to endPole
shift(n - 1, intermediatePole, startPole, endPole);
}
}
+ /**
+ * The main method that starts the Tower of Hanoi puzzle solution.
+ * It prompts the user for the number of discs and invokes the {@code shift}
+ * function
+ * to begin solving the puzzle.
+ *
+ * @param args Command-line arguments (not used in this case).
+ */
public static void main(String[] args) {
System.out.print("Enter number of discs on Pole 1: ");
Scanner scanner = new Scanner(System.in);
From 3bedefd1f09375d05c46cbe715e39fd5f37c2de8 Mon Sep 17 00:00:00 2001
From: Hardvan
Date: Thu, 3 Oct 2024 04:52:29 +0000
Subject: [PATCH 02/13] Update directory
---
DIRECTORY.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 6f63a88b085a..9766c2f83589 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -451,6 +451,8 @@
* [TowerOfHanoi](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/TowerOfHanoi.java)
* [TwoPointers](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/TwoPointers.java)
* [Verhoeff](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/Verhoeff.java)
+ * Recursion
+ * [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java)
* scheduling
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
@@ -893,6 +895,8 @@
* [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java)
* [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java)
* [WorstFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/WorstFitCPUTest.java)
+ * Recursion
+ * [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
* scheduling
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
From a04c2c3703f7b9f1680a5736206a16e186f52102 Mon Sep 17 00:00:00 2001
From: Hardik Pawar
Date: Thu, 3 Oct 2024 10:26:52 +0530
Subject: [PATCH 03/13] Add wiki link
---
src/main/java/com/thealgorithms/others/TowerOfHanoi.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
index c929ecd74dc9..4d6bac591750 100644
--- a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
+++ b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
@@ -9,6 +9,7 @@
* the puzzle.
* The main function interacts with the user to get the number of discs and
* calls the recursive {@code shift} function to perform the moves.
+ * Wikipedia: https://en.wikipedia.org/wiki/Tower_of_Hanoi
*/
final class TowerOfHanoi {
@@ -16,7 +17,7 @@ private TowerOfHanoi() {
}
/**
- * Recursively solves the Tower of Hanoi puzzle by moving discs between poles.
+ * Recursively solve the Tower of Hanoi puzzle by moving discs between poles.
*
* @param n The number of discs to move.
* @param startPole The name of the start pole.
From c2f6872ea3fe40ba57691b90f97a522334454bf1 Mon Sep 17 00:00:00 2001
From: Hardvan
Date: Fri, 4 Oct 2024 14:59:26 +0000
Subject: [PATCH 04/13] Update directory
---
DIRECTORY.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 24cd22f67d07..af5905d00747 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -238,6 +238,7 @@
* [KnapsackMemoization](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java)
* [LevenshteinDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LevenshteinDistance.java)
* [LongestAlternatingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java)
+ * [LongestArithmeticSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java)
* [LongestCommonSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestCommonSubsequence.java)
* [LongestIncreasingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java)
* [LongestPalindromicSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubsequence.java)
@@ -733,6 +734,7 @@
* [KnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java)
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
* [LongestAlternatingSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java)
+ * [LongestArithmeticSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java)
* [LongestIncreasingSubsequenceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java)
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)
From 10102c7a6560cf492112b01d814942f9aa2f00bc Mon Sep 17 00:00:00 2001
From: Hardik Pawar
Date: Fri, 4 Oct 2024 20:34:19 +0530
Subject: [PATCH 05/13] Fix comments
---
src/main/java/com/thealgorithms/others/TowerOfHanoi.java | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
index 4d6bac591750..f495dc4d1a76 100644
--- a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
+++ b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
@@ -49,8 +49,7 @@ public static void shift(int n, String startPole, String intermediatePole, Strin
/**
* The main method that starts the Tower of Hanoi puzzle solution.
* It prompts the user for the number of discs and invokes the {@code shift}
- * function
- * to begin solving the puzzle.
+ * function to begin solving the puzzle.
*
* @param args Command-line arguments (not used in this case).
*/
From c0dc63b6e5e64aa930c7e0b0d07a4e7957e00b96 Mon Sep 17 00:00:00 2001
From: Hardik Pawar
Date: Sat, 5 Oct 2024 19:29:25 +0530
Subject: [PATCH 06/13] Add unit tests for ToH
---
.../thealgorithms/others/TowerOfHanoi.java | 28 ++-------
.../others/TowerOfHanoiTest.java | 60 +++++++++++++++++++
2 files changed, 66 insertions(+), 22 deletions(-)
create mode 100644 src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java
diff --git a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
index f495dc4d1a76..4349091db7a0 100644
--- a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
+++ b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
@@ -1,6 +1,6 @@
package com.thealgorithms.others;
-import java.util.Scanner;
+import java.util.List;
/**
* The {@code TowerOfHanoi} class provides functionality to solve the Tower of
@@ -32,32 +32,16 @@ private TowerOfHanoi() {
* intermediate pole to the end pole.
*
*/
- public static void shift(int n, String startPole, String intermediatePole, String endPole) {
- // If n becomes zero, the program returns, ending the recursion.
+ public static void shift(int n, String startPole, String intermediatePole, String endPole, List result) {
if (n != 0) {
// Recursively move n-1 discs from startPole to intermediatePole
- shift(n - 1, startPole, endPole, intermediatePole);
+ shift(n - 1, startPole, endPole, intermediatePole, result);
- // Print the move of the nth disc from startPole to endPole
- System.out.format("Move %d from %s to %s%n", n, startPole, endPole);
+ // Add the move of the nth disc from startPole to endPole
+ result.add(String.format("Move %d from %s to %s", n, startPole, endPole));
// Recursively move the n-1 discs from intermediatePole to endPole
- shift(n - 1, intermediatePole, startPole, endPole);
+ shift(n - 1, intermediatePole, startPole, endPole, result);
}
}
-
- /**
- * The main method that starts the Tower of Hanoi puzzle solution.
- * It prompts the user for the number of discs and invokes the {@code shift}
- * function to begin solving the puzzle.
- *
- * @param args Command-line arguments (not used in this case).
- */
- public static void main(String[] args) {
- System.out.print("Enter number of discs on Pole 1: ");
- Scanner scanner = new Scanner(System.in);
- int numberOfDiscs = scanner.nextInt(); // input of number of discs on pole 1
- shift(numberOfDiscs, "Pole1", "Pole2", "Pole3"); // Shift function called
- scanner.close();
- }
}
diff --git a/src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java b/src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java
new file mode 100644
index 000000000000..56b775495db2
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java
@@ -0,0 +1,60 @@
+package com.thealgorithms.others;
+
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+import java.util.ArrayList;
+import java.util.List;
+
+public class TowerOfHanoiTest {
+
+ @Test
+ public void testHanoiWithOneDisc() {
+ List result = new ArrayList<>();
+ TowerOfHanoi.shift(1, "Pole1", "Pole2", "Pole3", result);
+
+ // Expected output for 1 disc
+ List expected = List.of("Move 1 from Pole1 to Pole3");
+ assertEquals(expected, result);
+ }
+
+ @Test
+ public void testHanoiWithTwoDiscs() {
+ List result = new ArrayList<>();
+ TowerOfHanoi.shift(2, "Pole1", "Pole2", "Pole3", result);
+
+ // Expected output for 2 discs
+ List expected = List.of(
+ "Move 1 from Pole1 to Pole2",
+ "Move 2 from Pole1 to Pole3",
+ "Move 1 from Pole2 to Pole3"
+ );
+ assertEquals(expected, result);
+ }
+
+ @Test
+ public void testHanoiWithThreeDiscs() {
+ List result = new ArrayList<>();
+ TowerOfHanoi.shift(3, "Pole1", "Pole2", "Pole3", result);
+
+ // Expected output for 3 discs
+ List 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"
+ );
+ assertEquals(expected, result);
+ }
+
+ @Test
+ public void testHanoiWithZeroDiscs() {
+ List result = new ArrayList<>();
+ TowerOfHanoi.shift(0, "Pole1", "Pole2", "Pole3", result);
+
+ // There should be no moves if there are 0 discs
+ assertTrue(result.isEmpty());
+ }
+}
From 65b930652c57a656bbad5d8526f8872f5222e0a7 Mon Sep 17 00:00:00 2001
From: Hardvan
Date: Sat, 5 Oct 2024 13:59:43 +0000
Subject: [PATCH 07/13] Update directory
---
DIRECTORY.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/DIRECTORY.md b/DIRECTORY.md
index af5905d00747..2a993baf6aa9 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -897,6 +897,7 @@
* [ReverseStackUsingRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java)
* [StringMatchFiniteAutomataTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/StringMatchFiniteAutomataTest.java)
* [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java)
+ * [TowerOfHanoiTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java)
* [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java)
* [WorstFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/WorstFitCPUTest.java)
* Recursion
From 69e69b5556ea4a0be65e407161518f5c824fa978 Mon Sep 17 00:00:00 2001
From: Hardik Pawar
Date: Sat, 5 Oct 2024 19:32:19 +0530
Subject: [PATCH 08/13] Fix clang errors
---
.../others/TowerOfHanoiTest.java | 22 +++++--------------
1 file changed, 6 insertions(+), 16 deletions(-)
diff --git a/src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java b/src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java
index 56b775495db2..ca9376dd48eb 100644
--- a/src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java
+++ b/src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java
@@ -1,9 +1,11 @@
package com.thealgorithms.others;
-import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
import java.util.ArrayList;
import java.util.List;
+import org.junit.jupiter.api.Test;
public class TowerOfHanoiTest {
@@ -23,11 +25,7 @@ public void testHanoiWithTwoDiscs() {
TowerOfHanoi.shift(2, "Pole1", "Pole2", "Pole3", result);
// Expected output for 2 discs
- List expected = List.of(
- "Move 1 from Pole1 to Pole2",
- "Move 2 from Pole1 to Pole3",
- "Move 1 from Pole2 to Pole3"
- );
+ List expected = List.of("Move 1 from Pole1 to Pole2", "Move 2 from Pole1 to Pole3", "Move 1 from Pole2 to Pole3");
assertEquals(expected, result);
}
@@ -37,15 +35,7 @@ public void testHanoiWithThreeDiscs() {
TowerOfHanoi.shift(3, "Pole1", "Pole2", "Pole3", result);
// Expected output for 3 discs
- List 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"
- );
+ List 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");
assertEquals(expected, result);
}
From 5d17a20b9b1ff1fe31ab4e43088b832d84e90536 Mon Sep 17 00:00:00 2001
From: Hardvan
Date: Sat, 5 Oct 2024 14:03:24 +0000
Subject: [PATCH 09/13] Update directory
---
DIRECTORY.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 3a4ade56e211..9fe5f07786bd 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -747,6 +747,7 @@
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
* [LongestAlternatingSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java)
* [LongestArithmeticSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java)
+ * [LongestCommonSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestCommonSubsequenceTest.java)
* [LongestIncreasingSubsequenceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java)
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)
From 146f3e9dd25d5b95f4968185ced57bfd4d0dc40c Mon Sep 17 00:00:00 2001
From: Hardik Pawar
Date: Sat, 5 Oct 2024 19:38:13 +0530
Subject: [PATCH 10/13] Fix comments
---
src/main/java/com/thealgorithms/others/TowerOfHanoi.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
index 4349091db7a0..a2e11e13e935 100644
--- a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
+++ b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
@@ -34,13 +34,13 @@ private TowerOfHanoi() {
*/
public static void shift(int n, String startPole, String intermediatePole, String endPole, List result) {
if (n != 0) {
- // Recursively move n-1 discs from startPole to intermediatePole
+ // Move n-1 discs from startPole to intermediatePole
shift(n - 1, startPole, endPole, intermediatePole, result);
// Add the move of the nth disc from startPole to endPole
result.add(String.format("Move %d from %s to %s", n, startPole, endPole));
- // Recursively move the n-1 discs from intermediatePole to endPole
+ // Move the n-1 discs from intermediatePole to endPole
shift(n - 1, intermediatePole, startPole, endPole, result);
}
}
From 49e42ac99447a97ef17cb23ad4eedcece68cdf6f Mon Sep 17 00:00:00 2001
From: Hardik Pawar
Date: Sun, 6 Oct 2024 18:08:24 +0530
Subject: [PATCH 11/13] Add suggested changes
---
.../thealgorithms/others/TowerOfHanoi.java | 36 ++++++++++++++-----
1 file changed, 27 insertions(+), 9 deletions(-)
diff --git a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
index a2e11e13e935..7017ed03f843 100644
--- a/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
+++ b/src/main/java/com/thealgorithms/others/TowerOfHanoi.java
@@ -3,12 +3,24 @@
import java.util.List;
/**
- * The {@code TowerOfHanoi} class provides functionality to solve the Tower of
- * Hanoi puzzle.
- * It uses recursion to move discs between poles and prints the steps to solve
- * the puzzle.
- * The main function interacts with the user to get the number of discs and
- * calls the recursive {@code shift} function to perform the moves.
+ * The {@code TowerOfHanoi} class provides a recursive solution to the Tower of Hanoi puzzle.
+ * This puzzle involves moving a set of discs from one pole to another, following specific rules:
+ * 1. Only one disc can be moved at a time.
+ * 2. A disc can only be placed on top of a larger disc.
+ * 3. All discs must start on one pole and end on another.
+ *
+ * This implementation recursively calculates the steps required to solve the puzzle and stores them
+ * in a provided list.
+ *
+ *
+ * For more information about the Tower of Hanoi, see
+ * Tower of Hanoi on Wikipedia.
+ *
+ *
+ * The {@code shift} method takes the number of discs and the names of the poles,
+ * and appends the steps required to solve the puzzle to the provided list.
+ * Time Complexity: O(2^n) - Exponential time complexity due to the recursive nature of the problem.
+ * Space Complexity: O(n) - Linear space complexity due to the recursion stack.
* Wikipedia: https://en.wikipedia.org/wiki/Tower_of_Hanoi
*/
final class TowerOfHanoi {
@@ -20,9 +32,10 @@ private TowerOfHanoi() {
* Recursively solve the Tower of Hanoi puzzle by moving discs between poles.
*
* @param n The number of discs to move.
- * @param startPole The name of the start pole.
- * @param intermediatePole The name of the intermediate pole.
- * @param endPole The name of the end pole.
+ * @param startPole The name of the start pole from which discs are moved.
+ * @param intermediatePole The name of the intermediate pole used as a temporary holding area.
+ * @param endPole The name of the end pole to which discs are moved.
+ * @param result A list to store the steps required to solve the puzzle.
*
*
* This method is called recursively to move n-1 discs
@@ -31,6 +44,11 @@ private TowerOfHanoi() {
* moves the n-1 discs from the
* intermediate pole to the end pole.
*
+ *
+ *
+ * Time Complexity: O(2^n) - Exponential time complexity due to the recursive nature of the problem.
+ * Space Complexity: O(n) - Linear space complexity due to the recursion stack.
+ *
*/
public static void shift(int n, String startPole, String intermediatePole, String endPole, List result) {
if (n != 0) {
From ba1b5482ad9e0745311748c89fa023834441219b Mon Sep 17 00:00:00 2001
From: alxkm
Date: Sat, 12 Oct 2024 19:00:16 +0000
Subject: [PATCH 12/13] Update directory
---
DIRECTORY.md | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/DIRECTORY.md b/DIRECTORY.md
index decb6e872e6f..e7a899104cdf 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -25,9 +25,11 @@
* bitmanipulation
* [BinaryPalindromeCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java)
* [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java)
+ * [BooleanAlgebraGates](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java)
* [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java)
* [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java)
* [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java)
+ * [FindNthBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/FindNthBit.java)
* [GrayCodeConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java)
* [HammingDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HammingDistance.java)
* [HigherLowerPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java)
@@ -54,6 +56,7 @@
* [CompositeLFSR](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java)
* [LFSR](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/LFSR.java)
* [Utils](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/Utils.java)
+ * [ADFGVXCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ADFGVXCipher.java)
* [AES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AES.java)
* [AESEncryption](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AESEncryption.java)
* [AffineCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AffineCipher.java)
@@ -133,6 +136,7 @@
* [FordFulkerson](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/FordFulkerson.java)
* [Graphs](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java)
* [HamiltonianCycle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java)
+ * [JohnsonsAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/JohnsonsAlgorithm.java)
* [KahnsAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java)
* [Kosaraju](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java)
* [Kruskal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Kruskal.java)
@@ -340,6 +344,7 @@
* [EulersFunction](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/EulersFunction.java)
* [Factorial](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Factorial.java)
* [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FactorialRecursion.java)
+ * [FastExponentiation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FastExponentiation.java)
* [FastInverseSqrt](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java)
* [FFT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FFT.java)
* [FFTBluestein](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FFTBluestein.java)
@@ -656,9 +661,11 @@
* bitmanipulation
* [BinaryPalindromeCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java)
* [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java)
+ * [BooleanAlgebraGatesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGatesTest.java)
* [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java)
* [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java)
* [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java)
+ * [FindNthBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/FindNthBitTest.java)
* [GrayCodeConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/GrayCodeConversionTest.java)
* [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HammingDistanceTest.java)
* [HigherLowerPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java)
@@ -682,6 +689,7 @@
* [A5CipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5CipherTest.java)
* [A5KeyStreamGeneratorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5KeyStreamGeneratorTest.java)
* [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
+ * [ADFGVXCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/ADFGVXCipherTest.java)
* [AESEncryptionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java)
* [AffineCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AffineCipherTest.java)
* [AtbashTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AtbashTest.java)
@@ -750,6 +758,7 @@
* [FloydWarshallTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/FloydWarshallTest.java)
* [FordFulkersonTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/FordFulkersonTest.java)
* [HamiltonianCycleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java)
+ * [JohnsonsAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/JohnsonsAlgorithmTest.java)
* [KosarajuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java)
* [TarjansAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithmTest.java)
* [WelshPowellTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/WelshPowellTest.java)
@@ -902,6 +911,7 @@
* [EulersFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/EulersFunctionTest.java)
* [FactorialRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java)
* [FactorialTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialTest.java)
+ * [FastExponentiationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FastExponentiationTest.java)
* [FastInverseSqrtTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FastInverseSqrtTests.java)
* [FFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FFTTest.java)
* [FibonacciJavaStreamsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FibonacciJavaStreamsTest.java)
From e5570861f6402029b7baae0eeddbb7e98bef4169 Mon Sep 17 00:00:00 2001
From: alxkm
Date: Sun, 13 Oct 2024 09:16:56 +0000
Subject: [PATCH 13/13] Update directory
---
DIRECTORY.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 2680f8ddb7fa..06307d4aca78 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -687,7 +687,7 @@
* [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java)
* [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java)
* [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java)
- * [Xs3ConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/Xs3ConversionTest.java)
+ * [Xs3ConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/Xs3ConversionTest.java)
* ciphers
* a5
* [A5CipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5CipherTest.java)