Skip to content

tower_of _hanoi.py #9938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
53 changes: 53 additions & 0 deletions tower_of _hanoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
def tower_of_hanoi(n: int, source_peg: str, auxiliary_peg: str, target_peg: str) -> None:
"""
Solve the Tower of Hanoi problem for 'n' disks.

Args:
n (int): The number of disks to move.
source_peg (str): The name of the source peg.
auxiliary_peg (str): The name of the auxiliary peg.
target_peg (str): The name of the target peg.

Returns:
None

Examples:
>>> tower_of_hanoi(1, 'A', 'B', 'C')
Move disk 1 from A to C

>>> tower_of_hanoi(3, 'A', 'B', 'C')
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
"""
if n == 1:
print(f"Move disk 1 from {source_peg} to {target_peg}")
return
tower_of_hanoi(n - 1, source_peg, target_peg, auxiliary_peg)
print(f"Move disk {n} from {source_peg} to {target_peg}")
tower_of_hanoi(n - 1, auxiliary_peg, source_peg, target_peg)

# Input from the user
if __name__ == "__main__":
n = int(input("Enter the number of disks: "))
source_peg = input("Enter the name of the source peg (e.g., 'A'): ").strip().upper()
auxiliary_peg = input("Enter the name of the auxiliary peg (e.g., 'B'): ").strip().upper()
target_peg = input("Enter the name of the target peg (e.g., 'C'): ").strip().upper()

# Ensure peg names are distinct
while source_peg == auxiliary_peg or source_peg == target_peg or auxiliary_peg == target_peg:
print("Peg names must be distinct. Please enter again.")
source_peg = input("Enter the name of the source peg: ").strip().upper()
auxiliary_peg = input("Enter the name of the auxiliary peg: ").strip().upper()
target_peg = input("Enter the name of the target peg: ").strip().upper()

# Call the tower_of_hanoi function with user input
tower_of_hanoi(n, source_peg, auxiliary_peg, target_peg)

For more:
https://github.com/TheAlgorithms/Python

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: tower_of _hanoi.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 52:9.
parser error: error at 51:8: expected one of !=, %, &, (, *, **, +, ,, -, ., /, //, :, ;, <, <<, <=, =, ==, >, >=, >>, @, NEWLINE, [, ^, and, if, in, is, not, or, |

 https://github.com/TheAlgorithms/Python
        ^

https://en.wikipedia.org/wiki/Tower_of_Hanoi