15,992,250 members
1.00/5 (1 vote)
See more:
Code for the Tower of Hanoi ist recursivly made out of curiosity i just wondered how it exactly works.

Python
```def hanoi(n, source, helper, target):
print ("hanoi( ", n, source, helper, target, " called")
if n > 0:
#move tower of size n - 1 to helper:
hanoi(n - 1, source, target, helper)
#move disk from source peg to target peg
if source[0]:
disk = source[0].pop()
print( "moving " + str(disk) + " from " + source[1] + " to " + target[1])
target[0].append(disk)
# move tower of size n-1 from helper to target
hanoi(n - 1, helper, source, target)    #This Line is interesting for me

source = ([4, 3, 2, 1], "source")
target = ([], "target")
helper = ([], "helper")
hanoi(len(source[0]), source, helper, target)

print (source, helper, target)```

What speicificlly: Does it simultaniously work on multiple recursions at the same time? i.e first one hanoi(n - 1, source, target, helper) till it reaches an n== 0 end.
Or does it take second one in consideration hanoi(n - 1, helper, source, target),
and about n does it take it in the consideration. And also how does the code change from source to target to helper, and how do they interact with each other i.e.: why sometimes it takes it from source to target, other time from helper to target?

What I have tried:

I tended to think than form both hanois n was independt, also that it changes automaticlly from source to helper to target.
Posted
Comments
Richard MacCutchan 4-Feb-18 9:17am
The comments tell you what it does.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 40 Pete O'Hanlon 35 M Imran Ansari 10 Dave Kreskowiak 0 Member 16361057 -16
 OriginalGriff 371 Pete O'Hanlon 205 CPallini 110 Dave Kreskowiak 60 CHill60 42

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900