Created
July 24, 2019 18:51
-
-
Save jatinsharrma/50b970dfe5d1dc64d9d18632cdf5a892 to your computer and use it in GitHub Desktop.
Tower of Hanoi
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
''' | |
Recursion Function | |
For 3 disk (Here S= Source, D= Destination,T= Temp) | |
3[S,D,T] | |
/ | \ | |
/ | \ | |
2[S,T,D] 1[S,D,T] 2[T,D,S] | |
/ | \ / | \ | |
/ | \ 1[T,S,D] 1[T,D,S] 1[S,D,T] | |
1[S,D,T] 1[S,T,D] 1[D,T,S] | |
''' | |
def tower_of_hanoi(n, source,destination,temp): | |
if(n==1): | |
disk=source.pop(0) #Removes the element in specified position | |
destination.insert(0,disk) #Inserts the given element in specified position | |
return | |
tower_of_hanoi(n-1, source, temp, destination) | |
disk=source.pop(0) | |
destination.insert(0,disk) | |
tower_of_hanoi(n-1, temp, destination, source) | |
return | |
source=["blue","green","orange"] | |
destination=[] | |
temp=[] | |
tower_of_hanoi(3, source, destination, temp) | |
print("Source:",source) | |
print("Destination:",destination) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment