Python Hanoi Towers - Understanding Recursion

I am completely new to python, and now I am looking at a tutorial on Hanoi towers and recursion. I thought I understood recursion until I gave this example:

def moveTower(height,fromPole, toPole, withPole):
    if height >= 1:
        moveTower(height-1,fromPole,withPole,toPole)
        moveDisk(fromPole,toPole)
        moveTower(height-1,withPole,toPole,fromPole)
    #print(withPole)

def moveDisk(fp,tp):
    print("moving disk from",fp,"to",tp)


moveTower(3,"A","B","C")

which prints the correct moves to solve the tower of the Hanoi problem with 3 disks: moving the disk from A to B moving the disk from A to C moving the disk from B to C moving the disk from A to B moving the disk from C to A moving the disk from C to B moving a drive from A to B

my question is how is this done !? can someone iterate over lines of code so that I understand how it prints the correct moves? I'm mostly confused about how the fp and tp values ​​can change from A to B to C. Sorry if this is a bit of a broad question! Any help would be greatly appreciated!

+4
5

, , print s, :

def moveTower(height,fromPole, toPole, withPole):
    if height >= 1:
        print( "    "*(3-height), "moveTower:", height, fromPole, toPole )
        moveTower(height-1,fromPole,withPole,toPole)
        moveDisk(fromPole,toPole,height)
        moveTower(height-1,withPole,toPole,fromPole)
    #print(withPole)

def moveDisk(fp,tp,height):
    print("    "*(4-height), "moving disk", "~"*(height), "from",fp,"to",tp)


moveTower(3,"A","B","C")

:

moveTower: 3 A B
     moveTower: 2 A C
         moveTower: 1 A B
             moving disk ~ from A to B
         moving disk ~~ from A to C
         moveTower: 1 B C
             moving disk ~ from B to C
     moving disk ~~~ from A to B
     moveTower: 2 C B
         moveTower: 1 C A
             moving disk ~ from C to A
         moving disk ~~ from C to B
         moveTower: 1 A B
             moving disk ~ from A to B
+2

. :

A|321
B|
C|

moveTower(2,fromA,toC, withB) :

A|3
B| 
C|21

moveDisk(fromA, toB)

A|
B|3
C|21

, , moveTower(2,fromC, toB)

A|
B|
C|321

: h-1 withPole, endPole h-1 endPole.

, h-1 .

moveTower(height-1,w,x), 3 .

, moveTower(height-2,y,z) .

: , ( " " ).

, height-1, 3 , . moveDisc - " " ( ), moveTower - ( n n+1).

+1

, , . , , ( ) -, "" , . , , , , . , . , , , . , , "" .

.

0

moveTower, . . (, ).

def seq_nummer():
    num = 0
    while True:
        num += 1
        yield num

seq_num = seq_nummer()

def moveTower(height, fromPole, toPole, withPole):
    print("seq: %d" % seq_num.next())
    print("height: %d, fromPole: %s, toPole: %s, withPole: %s" % (height, fromPole, toPole, withPole))
    if height >= 1:
        moveTower(height-1, fromPole, withPole, toPole)
        moveDisk(fromPole, toPole)
        moveTower(height-1, withPole, toPole, fromPole)

def moveDisk(fp,tp):
    print("moving disk from",fp,"to",tp)

moveTower(3,"A","B","C")
0
moveTower(height-1,fromPole,withPole,toPole)

, , .

moveDisk(fromPole,toPole)

. .

moveTower(height-1,withPole,toPole,fromPole)

, .

0

Source: https://habr.com/ru/post/1536762/


All Articles