Skip to content

Commit fd1fe56

Browse files
Create TowerOfHanoi.py
Tower of Hanoi using Python
1 parent cbc6235 commit fd1fe56

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

TowerOfHanoi.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Recursive Python function to solve the tower of hanoi --
2+
3+
def TowerOfHanoi(n , source, destination, auxiliary):
4+
if n==1:
5+
print ("Move disk 1 from source",source,"to destination",destination)
6+
return
7+
TowerOfHanoi(n-1, source, auxiliary, destination)
8+
print ("Move disk",n,"from source",source,"to destination",destination)
9+
TowerOfHanoi(n-1, auxiliary, destination, source)
10+
11+
# Driver code
12+
n = 4
13+
TowerOfHanoi(n,'A','B','C')
14+
# A, C, B are the name of rods

0 commit comments

Comments
 (0)