From c319d4edff6c636031a44125b89d72d7ad9adbc8 Mon Sep 17 00:00:00 2001 From: justanothercoder-hub Date: Wed, 21 Jan 2026 22:18:17 +0530 Subject: [PATCH 01/10] Added TowerofHanoi and its test file to recursion --- .../thealgorithms/recursion/TowerofHanoi.java | 76 +++++++++++++++++++ .../recursion/TowerofHanoiTest.java | 74 ++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 src/main/java/com/thealgorithms/recursion/TowerofHanoi.java create mode 100644 src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java diff --git a/src/main/java/com/thealgorithms/recursion/TowerofHanoi.java b/src/main/java/com/thealgorithms/recursion/TowerofHanoi.java new file mode 100644 index 000000000000..e3c57fefecbe --- /dev/null +++ b/src/main/java/com/thealgorithms/recursion/TowerofHanoi.java @@ -0,0 +1,76 @@ +package com.thealgorithms.recursion; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** + * TowerOfHanoi - Solves the classic Tower of Hanoi puzzle + * + * This algorithm uses recursion to move a stack of disks from a source rod to a + * destination rod, following the rules: + * 1. Only one disk can be moved at a time. + * 2. Each move consists of taking the upper disk from one of the stacks and + * placing it on top of another stack. + * 3. No disk may be placed on top of a smaller disk. + * + * Example: If n = 3, Source = 'A', Destination = 'C', Auxiliary = 'B' + * Resulting moves will guide disks from A to C using B. + * + * @author justanothercoder + * @see Tower of Hanoi + */ +public final class TowerofHanoi { + + private TowerofHanoi() { + + // Utility class + } + + /** + * Solves the Tower of Hanoi puzzle and returns the list of moves + * + * @param n number of disks + * @param source the source rod + * @param destination the destination rod + * @param auxiliary the auxiliary rod + * @return list of moves as strings + */ + public static List solveTowerOfHanoi(int n, char source, char destination, char auxiliary) { + List moves = new ArrayList<>(); + if(n<0){ + throw new IllegalArgumentException("Number of disks cannot be negative"); + } + moveDisks(n, source, destination, auxiliary, moves); + return moves; + } + + /** + * Recursive helper method to move disks + * + * @param n number of disks + * @param source the source rod + * @param destination the destination rod + * @param auxiliary the auxiliary rod + * @param moves list to record the moves + */ + private static void moveDisks(int n, char source, char destination, char auxiliary, List moves) { + if (n == 1) { + moves.add("Move disk 1 from rod " + source + " to rod " + destination); + return; + } + moveDisks(n - 1, source, auxiliary, destination, moves); + moves.add("Move disk " + n + " from rod " + source + " to rod " + destination); + moveDisks(n - 1, auxiliary, destination, source, moves); + } + + public static void main(String[] args){ + Scanner scanner = new Scanner(System.in); + System.out.print("Enter the number of disks: "); + int n = scanner.nextInt(); + List result = solveTowerOfHanoi(n, 'A', 'C', 'B'); + + for(String move: result){ + System.out.println(move); + } + } \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java b/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java new file mode 100644 index 000000000000..9e27ca570411 --- /dev/null +++ b/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java @@ -0,0 +1,74 @@ +package com.thealgorithms.recursion; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import java.util.Arrays; +import java.util.List; + +public class TowerOfHanoiTest { + + /** + * Test Case 1: Base Case (n = 1) + * Requirement: Explicitly requested. + * Logic: With only 1 disk, it should move directly from Source (A) to Destination (C). + */ + @Test + public void testBaseCase() { + List result = TowerOfHanoi.solveTowerOfHanoi(1, 'A', 'C', 'B'); + + // Assertion 1: Check size (2^1 - 1 = 1 move) + assertEquals(1, result.size(), "Should have exactly 1 move for 1 disk"); + + // Assertion 2: Verify the exact string + assertEquals("Move disk 1 from rod A to rod C", result.get(0)); + } + + /** + * Test Case 2: Small Recursion (n = 2) + * Logic: Tests the basic recursive step (Move 1 to Aux, Move 2 to Dest, Move 1 to Dest). + */ + @Test + public void testSmallRecursion() { + List result = TowerOfHanoi.solveTowerOfHanoi(2, 'A', 'C', 'B'); + + // Assertion 1: Check size (2^2 - 1 = 3 moves) + assertEquals(3, result.size()); + + // Assertion 2: Verify the exact sequence of moves + List expected = Arrays.asList( + "Move disk 1 from rod A to rod B", // Small disk to Aux + "Move disk 2 from rod A to rod C", // Big disk to Dest + "Move disk 1 from rod B to rod C" // Small disk to Dest + ); + + assertEquals(expected, result, "Sequence of moves for 2 disks is incorrect"); + } + + /** + * Test Case 3: Standard Case (n = 3) + * Logic: Verifies a slightly larger recursion to ensure the stack depth works correctly. + */ + @Test + public void testStandardCase() { + List result = TowerOfHanoi.solveTowerOfHanoi(3, 'A', 'C', 'B'); + + // Assertion 1: Check size (2^3 - 1 = 7 moves) + assertEquals(7, result.size()); + + // Assertion 2: Verify start and end moves specifically + assertEquals("Move disk 1 from rod A to rod C", result.get(0)); // First move + assertEquals("Move disk 1 from rod A to rod C", result.get(6)); // Last move + } + + /** + * Extra Test Case: Negative Input + * Logic: Ensures your "Defensive Programming" check works. + */ + @Test + public void testNegativeInput() { + assertThrows(IllegalArgumentException.class, () -> { + TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); + }, "Should throw exception for negative disks"); + } +} \ No newline at end of file From 1488417b622239af1525966e5d60f7848a5ce93d Mon Sep 17 00:00:00 2001 From: justanothercoder-hub Date: Wed, 21 Jan 2026 22:48:28 +0530 Subject: [PATCH 02/10] Fix filename capitalization to TowerOfHanoi --- .../recursion/{TowerofHanoi.java => TowerOfHanoi.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/com/thealgorithms/recursion/{TowerofHanoi.java => TowerOfHanoi.java} (100%) diff --git a/src/main/java/com/thealgorithms/recursion/TowerofHanoi.java b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java similarity index 100% rename from src/main/java/com/thealgorithms/recursion/TowerofHanoi.java rename to src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java From 3dc5f8eb6a25e9fb9fd884658b5579c122c078f2 Mon Sep 17 00:00:00 2001 From: justanothercoder-hub Date: Wed, 21 Jan 2026 23:52:59 +0530 Subject: [PATCH 03/10] Fixed some errors according to build format --- .../com/thealgorithms/recursion/TowerOfHanoi.java | 9 +++++---- .../thealgorithms/recursion/TowerofHanoiTest.java | 14 +++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java index e3c57fefecbe..69750bcd0ad3 100644 --- a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java +++ b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java @@ -38,7 +38,7 @@ private TowerofHanoi() { */ public static List solveTowerOfHanoi(int n, char source, char destination, char auxiliary) { List moves = new ArrayList<>(); - if(n<0){ + if(n < 0) { throw new IllegalArgumentException("Number of disks cannot be negative"); } moveDisks(n, source, destination, auxiliary, moves); @@ -64,13 +64,14 @@ private static void moveDisks(int n, char source, char destination, char auxilia moveDisks(n - 1, auxiliary, destination, source, moves); } - public static void main(String[] args){ + public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of disks: "); int n = scanner.nextInt(); List result = solveTowerOfHanoi(n, 'A', 'C', 'B'); - for(String move: result){ + for(String move : result){ System.out.println(move); } - } \ No newline at end of file + } + } diff --git a/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java b/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java index 9e27ca570411..e1f273ec6961 100644 --- a/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java +++ b/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java @@ -1,10 +1,12 @@ package com.thealgorithms.recursion; -import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Arrays; import java.util.List; +import org.junit.jupiter.api.Test; public class TowerOfHanoiTest { @@ -36,10 +38,9 @@ public void testSmallRecursion() { assertEquals(3, result.size()); // Assertion 2: Verify the exact sequence of moves - List expected = Arrays.asList( - "Move disk 1 from rod A to rod B", // Small disk to Aux + List expected = Arrays.asList("Move disk 1 from rod A to rod B", // Small disk to Aux "Move disk 2 from rod A to rod C", // Big disk to Dest - "Move disk 1 from rod B to rod C" // Small disk to Dest + "Move disk 1 from rod B to rod C" // Small disk to Dest ); assertEquals(expected, result, "Sequence of moves for 2 disks is incorrect"); @@ -67,8 +68,7 @@ public void testStandardCase() { */ @Test public void testNegativeInput() { - assertThrows(IllegalArgumentException.class, () -> { - TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); - }, "Should throw exception for negative disks"); + assertThrows(IllegalArgumentException.class, () -> { TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); }, +"Should throw exception for negative disks"); } } \ No newline at end of file From 05435c6e34862bc0ad7598d88c8cfffdb9ddb2ab Mon Sep 17 00:00:00 2001 From: justanothercoder-hub Date: Thu, 22 Jan 2026 00:06:32 +0530 Subject: [PATCH 04/10] Removed Main method and adjusted the test file --- .../thealgorithms/recursion/TowerOfHanoi.java | 22 ++++-------------- .../recursion/TowerofHanoiTest.java | 23 ++++++++----------- 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java index 69750bcd0ad3..fbe6639120ad 100644 --- a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java +++ b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java @@ -2,7 +2,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Scanner; /** * TowerOfHanoi - Solves the classic Tower of Hanoi puzzle @@ -17,12 +16,12 @@ * Example: If n = 3, Source = 'A', Destination = 'C', Auxiliary = 'B' * Resulting moves will guide disks from A to C using B. * - * @author justanothercoder + * @author justanothercoder-hub * @see Tower of Hanoi */ -public final class TowerofHanoi { +public final class TowerOfHanoi { - private TowerofHanoi() { + private TowerOfHanoi() { // Utility class } @@ -38,7 +37,7 @@ private TowerofHanoi() { */ public static List solveTowerOfHanoi(int n, char source, char destination, char auxiliary) { List moves = new ArrayList<>(); - if(n < 0) { + if (n < 0) { throw new IllegalArgumentException("Number of disks cannot be negative"); } moveDisks(n, source, destination, auxiliary, moves); @@ -63,15 +62,4 @@ private static void moveDisks(int n, char source, char destination, char auxilia moves.add("Move disk " + n + " from rod " + source + " to rod " + destination); moveDisks(n - 1, auxiliary, destination, source, moves); } - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - System.out.print("Enter the number of disks: "); - int n = scanner.nextInt(); - List result = solveTowerOfHanoi(n, 'A', 'C', 'B'); - - for(String move : result){ - System.out.println(move); - } - } - } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java b/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java index e1f273ec6961..d09605c0bd3d 100644 --- a/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java +++ b/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java @@ -1,6 +1,5 @@ package com.thealgorithms.recursion; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -12,16 +11,15 @@ public class TowerOfHanoiTest { /** * Test Case 1: Base Case (n = 1) - * Requirement: Explicitly requested. * Logic: With only 1 disk, it should move directly from Source (A) to Destination (C). */ @Test public void testBaseCase() { List result = TowerOfHanoi.solveTowerOfHanoi(1, 'A', 'C', 'B'); - + // Assertion 1: Check size (2^1 - 1 = 1 move) assertEquals(1, result.size(), "Should have exactly 1 move for 1 disk"); - + // Assertion 2: Verify the exact string assertEquals("Move disk 1 from rod A to rod C", result.get(0)); } @@ -33,16 +31,16 @@ public void testBaseCase() { @Test public void testSmallRecursion() { List result = TowerOfHanoi.solveTowerOfHanoi(2, 'A', 'C', 'B'); - + // Assertion 1: Check size (2^2 - 1 = 3 moves) assertEquals(3, result.size()); - + // Assertion 2: Verify the exact sequence of moves List expected = Arrays.asList("Move disk 1 from rod A to rod B", // Small disk to Aux "Move disk 2 from rod A to rod C", // Big disk to Dest "Move disk 1 from rod B to rod C" // Small disk to Dest ); - + assertEquals(expected, result, "Sequence of moves for 2 disks is incorrect"); } @@ -53,22 +51,21 @@ public void testSmallRecursion() { @Test public void testStandardCase() { List result = TowerOfHanoi.solveTowerOfHanoi(3, 'A', 'C', 'B'); - + // Assertion 1: Check size (2^3 - 1 = 7 moves) assertEquals(7, result.size()); - + // Assertion 2: Verify start and end moves specifically assertEquals("Move disk 1 from rod A to rod C", result.get(0)); // First move assertEquals("Move disk 1 from rod A to rod C", result.get(6)); // Last move } /** - * Extra Test Case: Negative Input - * Logic: Ensures your "Defensive Programming" check works. + * Illegal Input Case: Negative Input + * Logic: Ensures the exception is thrown when n is negative. */ @Test public void testNegativeInput() { - assertThrows(IllegalArgumentException.class, () -> { TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); }, -"Should throw exception for negative disks"); + assertThrows(IllegalArgumentException.class, () -> { TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); }, "Should throw exception for negative disks"); } } \ No newline at end of file From a71f743119975e5dcbe72c7d760c7ab88ffa959a Mon Sep 17 00:00:00 2001 From: justanothercoder-hub Date: Thu, 22 Jan 2026 00:17:13 +0530 Subject: [PATCH 05/10] Fixed some errors --- src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java index fbe6639120ad..e7d85151ad29 100644 --- a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java +++ b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java @@ -22,7 +22,6 @@ public final class TowerOfHanoi { private TowerOfHanoi() { - // Utility class } @@ -36,10 +35,10 @@ private TowerOfHanoi() { * @return list of moves as strings */ public static List solveTowerOfHanoi(int n, char source, char destination, char auxiliary) { - List moves = new ArrayList<>(); if (n < 0) { throw new IllegalArgumentException("Number of disks cannot be negative"); } + List moves = new ArrayList<>(); moveDisks(n, source, destination, auxiliary, moves); return moves; } From 3bf37fdc5a58600a1dc77a64d029a5a2adca091a Mon Sep 17 00:00:00 2001 From: justanothercoder-hub Date: Thu, 22 Jan 2026 00:27:15 +0530 Subject: [PATCH 06/10] Changed file name --- .../recursion/{TowerofHanoiTest.java => TowerOfHanoiTest.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/test/java/com/thealgorithms/recursion/{TowerofHanoiTest.java => TowerOfHanoiTest.java} (100%) diff --git a/src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java b/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java similarity index 100% rename from src/test/java/com/thealgorithms/recursion/TowerofHanoiTest.java rename to src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java From 73c938de0f92d852f8dc71d484f21dd8f8e23287 Mon Sep 17 00:00:00 2001 From: justanothercoder-hub Date: Thu, 22 Jan 2026 00:37:53 +0530 Subject: [PATCH 07/10] Fixed minor issues in test file --- .../thealgorithms/recursion/TowerOfHanoi.java | 2 +- .../recursion/TowerOfHanoiTest.java | 42 ++++--------------- 2 files changed, 8 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java index e7d85151ad29..0fd7be3177d6 100644 --- a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java +++ b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java @@ -61,4 +61,4 @@ private static void moveDisks(int n, char source, char destination, char auxilia moves.add("Move disk " + n + " from rod " + source + " to rod " + destination); moveDisks(n - 1, auxiliary, destination, source, moves); } -} \ No newline at end of file + } \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java b/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java index d09605c0bd3d..96d7bb882637 100644 --- a/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java +++ b/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java @@ -9,63 +9,35 @@ public class TowerOfHanoiTest { - /** - * Test Case 1: Base Case (n = 1) - * Logic: With only 1 disk, it should move directly from Source (A) to Destination (C). - */ @Test public void testBaseCase() { List result = TowerOfHanoi.solveTowerOfHanoi(1, 'A', 'C', 'B'); - - // Assertion 1: Check size (2^1 - 1 = 1 move) assertEquals(1, result.size(), "Should have exactly 1 move for 1 disk"); - - // Assertion 2: Verify the exact string assertEquals("Move disk 1 from rod A to rod C", result.get(0)); } - /** - * Test Case 2: Small Recursion (n = 2) - * Logic: Tests the basic recursive step (Move 1 to Aux, Move 2 to Dest, Move 1 to Dest). - */ @Test public void testSmallRecursion() { List result = TowerOfHanoi.solveTowerOfHanoi(2, 'A', 'C', 'B'); - - // Assertion 1: Check size (2^2 - 1 = 3 moves) assertEquals(3, result.size()); - - // Assertion 2: Verify the exact sequence of moves - List expected = Arrays.asList("Move disk 1 from rod A to rod B", // Small disk to Aux - "Move disk 2 from rod A to rod C", // Big disk to Dest - "Move disk 1 from rod B to rod C" // Small disk to Dest + List expected = Arrays.asList( + "Move disk 1 from rod A to rod B", + "Move disk 2 from rod A to rod C", + "Move disk 1 from rod B to rod C" ); - assertEquals(expected, result, "Sequence of moves for 2 disks is incorrect"); } - /** - * Test Case 3: Standard Case (n = 3) - * Logic: Verifies a slightly larger recursion to ensure the stack depth works correctly. - */ @Test public void testStandardCase() { List result = TowerOfHanoi.solveTowerOfHanoi(3, 'A', 'C', 'B'); - - // Assertion 1: Check size (2^3 - 1 = 7 moves) assertEquals(7, result.size()); - - // Assertion 2: Verify start and end moves specifically - assertEquals("Move disk 1 from rod A to rod C", result.get(0)); // First move - assertEquals("Move disk 1 from rod A to rod C", result.get(6)); // Last move + assertEquals("Move disk 1 from rod A to rod C", result.get(0)); + assertEquals("Move disk 1 from rod A to rod C", result.get(6)); } - /** - * Illegal Input Case: Negative Input - * Logic: Ensures the exception is thrown when n is negative. - */ @Test public void testNegativeInput() { - assertThrows(IllegalArgumentException.class, () -> { TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); }, "Should throw exception for negative disks"); + assertThrows(IllegalArgumentException.class, () -> TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'), "Should throw exception for negative disks"); } } \ No newline at end of file From 426e0f38fb99411cdc69dd38c62062845a4ed0ff Mon Sep 17 00:00:00 2001 From: justanothercoder-hub Date: Thu, 22 Jan 2026 00:45:56 +0530 Subject: [PATCH 08/10] Fix formatting issue --- .../java/com/thealgorithms/recursion/TowerOfHanoi.java | 4 ++-- .../com/thealgorithms/recursion/TowerOfHanoiTest.java | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java index 0fd7be3177d6..ff5f7d45acd9 100644 --- a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java +++ b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java @@ -35,10 +35,10 @@ private TowerOfHanoi() { * @return list of moves as strings */ public static List solveTowerOfHanoi(int n, char source, char destination, char auxiliary) { + List moves = new ArrayList<>(); if (n < 0) { throw new IllegalArgumentException("Number of disks cannot be negative"); } - List moves = new ArrayList<>(); moveDisks(n, source, destination, auxiliary, moves); return moves; } @@ -61,4 +61,4 @@ private static void moveDisks(int n, char source, char destination, char auxilia moves.add("Move disk " + n + " from rod " + source + " to rod " + destination); moveDisks(n - 1, auxiliary, destination, source, moves); } - } \ No newline at end of file +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java b/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java index 96d7bb882637..c46c9d4dcb9e 100644 --- a/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java +++ b/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java @@ -20,11 +20,7 @@ public void testBaseCase() { public void testSmallRecursion() { List result = TowerOfHanoi.solveTowerOfHanoi(2, 'A', 'C', 'B'); assertEquals(3, result.size()); - List expected = Arrays.asList( - "Move disk 1 from rod A to rod B", - "Move disk 2 from rod A to rod C", - "Move disk 1 from rod B to rod C" - ); + List expected = Arrays.asList("Move disk 1 from rod A to rod B", "Move disk 2 from rod A to rod C", "Move disk 1 from rod B to rod C"); assertEquals(expected, result, "Sequence of moves for 2 disks is incorrect"); } @@ -38,6 +34,6 @@ public void testStandardCase() { @Test public void testNegativeInput() { - assertThrows(IllegalArgumentException.class, () -> TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'), "Should throw exception for negative disks"); + assertThrows(IllegalArgumentException.class, () -> { TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); }, "Should throw exception for negative disks"); } } \ No newline at end of file From a93b60ecf49621c87d774d9ce0fab14c0f852441 Mon Sep 17 00:00:00 2001 From: justanothercoder-hub Date: Thu, 22 Jan 2026 00:56:09 +0530 Subject: [PATCH 09/10] Added missing line at the end --- src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java | 2 +- .../java/com/thealgorithms/recursion/TowerOfHanoiTest.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java index ff5f7d45acd9..45471843e998 100644 --- a/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java +++ b/src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java @@ -61,4 +61,4 @@ private static void moveDisks(int n, char source, char destination, char auxilia moves.add("Move disk " + n + " from rod " + source + " to rod " + destination); moveDisks(n - 1, auxiliary, destination, source, moves); } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java b/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java index c46c9d4dcb9e..b1870b674b81 100644 --- a/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java +++ b/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java @@ -36,4 +36,5 @@ public void testStandardCase() { public void testNegativeInput() { assertThrows(IllegalArgumentException.class, () -> { TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); }, "Should throw exception for negative disks"); } -} \ No newline at end of file +} + From c00563fca392190f0da61e4a399859be3117ee76 Mon Sep 17 00:00:00 2001 From: justanothercoder-hub Date: Thu, 22 Jan 2026 01:01:43 +0530 Subject: [PATCH 10/10] Fix file endings to have exactly one newline --- src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java b/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java index b1870b674b81..a82e844ed917 100644 --- a/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java +++ b/src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java @@ -37,4 +37,3 @@ public void testNegativeInput() { assertThrows(IllegalArgumentException.class, () -> { TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); }, "Should throw exception for negative disks"); } } -