Skip to content

Commit a4afe1b

Browse files
authored
restructure
1 parent 28b5584 commit a4afe1b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

hanoi tower.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11

22
# coding: utf-8
33

4-
# In[ ]:
4+
# In[1]:
55

66

77
#hanoi tower is the classic recursion
88
#the logic behind it is amazing
99
#rules can be seen from this website:
1010
# https://en.wikipedia.org/wiki/Tower_of_Hanoi
1111

12+
13+
# In[2]:
14+
15+
1216
def hanoi(n,a,b,c):
17+
1318
#rule states that each time we can only move one element
14-
#so when the recusion reaches to base case 1
19+
#so when the recursion reaches to base case 1
1520
#we print the movement of elements from a to c
1621
if n==1:
1722
print(a,'-',c)
@@ -22,13 +27,21 @@ def hanoi(n,a,b,c):
2227
#note that we set print a to c when n reaches one
2328
#so in this case we reorder the function, replace c with column b where elements actually move towards
2429
hanoi(n-1,a,c,b)
30+
2531
#the second step is to move the base case from column a to column c
2632
#we are only moving base case, thats why n=1
2733
hanoi(1,a,b,c)
34+
2835
#final step would be move everything above base case from column b to column c
2936
hanoi(n-1,b,a,c)
3037

38+
39+
# In[3]:
40+
41+
3142
hanoi(4,'a','b','c')
32-
# the best explanation i have discovered should be
43+
44+
45+
# the best explanation should be
3346
# https://www.python-course.eu/towers_of_hanoi.php
3447

0 commit comments

Comments
 (0)