Skip to content

Commit 44c3c16

Browse files
Create tower_of_hanoi.py
1 parent 39345c9 commit 44c3c16

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

DSA 450 GFG/tower_of_hanoi.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# we have to transfer n disks from rod A to C using the auxilliary rod B
2+
def tower_of_hanoi(n, A, B, C):
3+
if(n>0):
4+
tower_of_hanoi(n-1, A, C, B)
5+
print("Moving disk ",n," from rod ", A, " to rod ", C)
6+
tower_of_hanoi(n-1, B, A, C)
7+
8+
n= int(input())
9+
tower_of_hanoi(n, 'A','B','C')

0 commit comments

Comments
 (0)