Now I'm trying to understand the algorithm for solving the problem about the tower of Hanoi, here's the solution code:

And here is the result:

I superficially familiar with python, but recursion's in Africa and recursion: first

The algorithm I realized that to move n-1 from the main shaft to a temporary, then the last disc in the main rod to move to the destination and move all temporary in the end, but I do not understand how the function works inside.

Bought now the book "towers of Hanoi" to understand.

```
def moveTower(height,fromPole, toPole, withPole):
if height >= 1:
print(fromPole, toPole, withPole, height)
moveTower(height-1,fromPole,withPole,toPole)
moveDisk(fromPole,toPole, height)
print(fromPole, toPole, withPole, height)
moveTower(height-1,withPole,toPole,fromPole)
def moveDisk(fp,tp, height):
print("moving disk from",fp,"to",tp)
print("height", height)
moveTower(3,"A","B","C")
```

And here is the result:

```
A B C 3
A C B 2
A B C 1
moving disk from A to B
height 1
A B C 1
moving disk from A to C
height 2
A C B 2
B C A 1
moving disk from B to C
height 1
B C A 1
moving disk from A to B
height 3
A B C 3
C B A 2
C A B 1
moving disk from C to A
height 1
C A B 1
moving disk from C to B
height 2
C B A 2
A B C 1
moving disk from A to B
height 1
A B C 1
```

I superficially familiar with python, but recursion's in Africa and recursion: first

`print(fromPole, toPole, withPole, height)`

, which is in row 3, reports all clear. Once triggered `moveTower(height-1,fromPole,withPole,toPole)`

fromPole = A, withPole = C, toPole = 'B'? And why the third time in `print(fromPole, toPole, withPole, height)`

displays `A B C 1`

? Why do these values change?The algorithm I realized that to move n-1 from the main shaft to a temporary, then the last disc in the main rod to move to the destination and move all temporary in the end, but I do not understand how the function works inside.

Bought now the book "towers of Hanoi" to understand.

asked July 8th 19 at 16:10

2 answers

answered on July 8th 19 at 16:12

For the future:

Download**thonny** - very handy IDE for such tasks. Of course to write it in a web application is not much, but to look for understanding of solving the problem the most.

Download

answered on July 8th 19 at 16:14

Tried debajit?

Added print - kevon_Torphy commented on July 8th 19 at 16:17

Find more questions by tags PythonAlgorithmsRecursion