-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Added new file 'tower_of_hanoi.py' in divide and conquer folder #13605
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
Open
GOURAV-SAHA007
wants to merge
6
commits into
TheAlgorithms:master
Choose a base branch
from
GOURAV-SAHA007:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−0
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
618f6e1
Added new file in divide and conquer folder
GOURAV-SAHA007 67c2a36
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6caae52
Fix formatting of error message for invalid input
GOURAV-SAHA007 978317c
Refactor tower_of_hanoi function parameters
GOURAV-SAHA007 154785b
Refactor doctest output line in tower_of_hanoi.py (E501 fixed)
GOURAV-SAHA007 8079cc2
Refactor doctest output and error handling
GOURAV-SAHA007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| """ | ||
| Towers of Hanoi is a mathematical puzzle that is solved using a recursive | ||
| divide-and-conquer strategy. | ||
|
|
||
| It moves a stack of disks from a source pole to a destination pole, | ||
| using an auxiliary pole, subject to these rules: | ||
| 1. Only one disk can be moved at a time. | ||
| 2. Each move consists of taking the upper disk from one stack and | ||
| placing it on top of another stack or an empty pole. | ||
| 3. No disk may be placed on top of a smaller disk. | ||
|
|
||
| More information: | ||
| https://en.wikipedia.org/wiki/Tower_of_Hanoi | ||
| """ | ||
|
|
||
|
|
||
| def tower_of_hanoi(n: int, source: str = "A", auxiliary: str = "B", destination: str = "C") -> list[tuple[str, str]]: | ||
| """ | ||
| Pure python implementation of the Towers of Hanoi puzzle (recursive version), | ||
| returning the sequence of moves required to solve the puzzle. | ||
|
|
||
| >>> tower_of_hanoi(1) | ||
| [('A', 'C')] | ||
| >>> tower_of_hanoi(2) | ||
| [('A', 'B'), ('A', 'C'), ('B', 'C')] | ||
| >>> tower_of_hanoi(3) | ||
| [('A', 'C'), ('A', 'B'), ('C', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('A', 'C')] | ||
| >>> tower_of_hanoi(0) | ||
| [] | ||
| """ | ||
| moves = [] | ||
|
|
||
| def solve_hanoi(n: int, source: str, auxiliary: str, destination: str): | ||
|
GOURAV-SAHA007 marked this conversation as resolved.
Outdated
GOURAV-SAHA007 marked this conversation as resolved.
Outdated
|
||
| """Recursive helper function to generate the moves.""" | ||
| if n == 0: | ||
| return | ||
|
|
||
| # 1. Move n-1 disks from Source to Auxiliary, using Destination as auxiliary. | ||
| # This is the "Divide" step. | ||
| solve_hanoi(n - 1, source, destination, auxiliary) | ||
|
|
||
| # 2. Move the largest disk (n) from Source to Destination. | ||
| # This is the "Conquer" step (base step of the recursion). | ||
| moves.append((source, destination)) | ||
|
|
||
| # 3. Move n-1 disks from Auxiliary to Destination, using Source as auxiliary. | ||
| # This is the "Combine" step. | ||
| solve_hanoi(n - 1, auxiliary, source, destination) | ||
|
|
||
| solve_hanoi(n, source, auxiliary, destination) | ||
| return moves | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| n_disks = int(input("Enter the number of disks for the Tower of Hanoi: ").strip()) | ||
| if n_disks < 0: | ||
| print("Please enter a non-negative number of disks.") | ||
| else: | ||
| all_moves = tower_of_hanoi(n_disks) | ||
| print(f"\nTotal moves required: {len(all_moves)}") | ||
| print("Sequence of Moves (Source -> Destination):") | ||
| for move in all_moves: | ||
| print(f"Move disk from {move[0]} to {move[1]}") | ||
| except ValueError: | ||
| print("Invalid input. Please enter an integer.") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.