Skip to content

Commit 978317c

Browse files
Refactor tower_of_hanoi function parameters
1 parent 6caae52 commit 978317c

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

divide_and_conquer/tower_of_hanoi.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
def tower_of_hanoi(
18-
n: int, source: str = "A", auxiliary: str = "B", destination: str = "C"
18+
num_disks: int, source: str = "A", auxiliary: str = "B", destination: str = "C"
1919
) -> list[tuple[str, str]]:
2020
"""
2121
Pure python implementation of the Towers of Hanoi puzzle (recursive version),
@@ -32,36 +32,53 @@ def tower_of_hanoi(
3232
"""
3333
moves = []
3434

35-
def solve_hanoi(n: int, source: str, auxiliary: str, destination: str):
36-
"""Recursive helper function to generate the moves."""
37-
if n == 0:
35+
def solve_hanoi(
36+
num_disks: int, source: str, auxiliary: str, destination: str
37+
) -> None:
38+
"""
39+
Recursive helper function to generate the moves and append them to the 'moves' list.
40+
41+
>>> moves_test = []
42+
>>> def solve_hanoi_test(num_disks, source, auxiliary, destination):
43+
... if num_disks == 0:
44+
... return
45+
... solve_hanoi_test(num_disks - 1, source, destination, auxiliary)
46+
... moves_test.append((source, destination))
47+
... solve_hanoi_test(num_disks - 1, auxiliary, source, destination)
48+
>>> solve_hanoi_test(2, "S", "A", "D")
49+
>>> moves_test
50+
[('S', 'A'), ('S', 'D'), ('A', 'D')]
51+
"""
52+
if num_disks == 0:
3853
return
3954

4055
# 1. Move n-1 disks from Source to Auxiliary, using Destination as auxiliary.
4156
# This is the "Divide" step.
42-
solve_hanoi(n - 1, source, destination, auxiliary)
57+
solve_hanoi(num_disks - 1, source, destination, auxiliary)
4358

4459
# 2. Move the largest disk (n) from Source to Destination.
4560
# This is the "Conquer" step (base step of the recursion).
4661
moves.append((source, destination))
4762

4863
# 3. Move n-1 disks from Auxiliary to Destination, using Source as auxiliary.
4964
# This is the "Combine" step.
50-
solve_hanoi(n - 1, auxiliary, source, destination)
65+
solve_hanoi(num_disks - 1, auxiliary, source, destination)
5166

52-
solve_hanoi(n, source, auxiliary, destination)
67+
solve_hanoi(num_disks, source, auxiliary, destination)
5368
return moves
5469

5570

5671
if __name__ == "__main__":
5772
try:
58-
n_disks = int(
59-
input("Enter the number of disks for the Tower of Hanoi: ").strip()
60-
)
61-
if n_disks < 0:
73+
n_disks_input = input(
74+
"Enter the number of disks for the Tower of Hanoi: "
75+
).strip()
76+
num_disks = int(n_disks_input)
77+
78+
if num_disks < 0:
6279
print("Please enter a non-negative number of disks.")
6380
else:
64-
all_moves = tower_of_hanoi(n_disks)
81+
all_moves = tower_of_hanoi(num_disks)
6582
print(f"\nTotal moves required: {len(all_moves)}")
6683
print("Sequence of Moves (Source -> Destination):")
6784
for move in all_moves:

0 commit comments

Comments
 (0)