Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions backtracking/tower_of_hanoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
def tower_of_hanoi(n, source, destination, auxiliary):
"""
Solves the Tower of Hanoi problem using backtracking.

Args:
n: The number of disks to be moved.
source: The index of the source peg.
destination: The index of the destination peg.
auxiliary: The index of the auxiliary peg.

Returns:
None
"""

if n == 1:
print(f"Move disk 1 from peg {source} to peg {destination}")
return

tower_of_hanoi(n - 1, source, auxiliary, destination)
print(f"Move disk {n} from peg {source} to peg {destination}")
tower_of_hanoi(n - 1, auxiliary, destination, source)


# Example usage:
n = 3 # Number of disks
tower_of_hanoi(n, 1, 3, 2)

# Explanation:

# tower_of_hanoi function:

# Takes four arguments:
# n: The number of disks to be moved.
# source: The index of the source peg.
# destination: The index of the destination peg.
# auxiliary: The index of the auxiliary peg.
# If n is 1, it means there's only one disk to move, so it directly prints the move.
# Otherwise, it recursively calls itself:
# Moves n-1 disks from the source to the auxiliary peg using the destination as the temporary peg.

Check failure on line 39 in backtracking/tower_of_hanoi.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/tower_of_hanoi.py:39:89: E501 Line too long (98 > 88)
# Moves the largest disk (n) from the source to the destination.
# Moves the n-1 disks from the auxiliary to the destination using the source as the temporary peg.

Check failure on line 41 in backtracking/tower_of_hanoi.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/tower_of_hanoi.py:41:89: E501 Line too long (98 > 88)
# Example usage:

# Sets n to 3 (the number of disks).
# Calls the tower_of_hanoi function with the initial pegs: source (1), destination (3), and auxiliary (2).

Check failure on line 45 in backtracking/tower_of_hanoi.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/tower_of_hanoi.py:45:89: E501 Line too long (106 > 88)
# Output:

# Move disk 1 from peg 1 to peg 3
# Move disk 2 from peg 1 to peg 2
# Move disk 1 from peg 3 to peg 2
# Move disk 3 from peg 1 to peg 3
# Move disk 1 from peg 2 to peg 1
# Move disk 2 from peg 2 to peg 3
# Move disk 1 from peg 1 to peg
# 3
# This output demonstrates the correct sequence of moves to solve the Tower of Hanoi problem with 3 disks.

Check failure on line 56 in backtracking/tower_of_hanoi.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/tower_of_hanoi.py:56:89: E501 Line too long (106 > 88)
Loading