Code for the Tower of Hanoi ist recursivly made out of curiosity i just wondered how it exactly works.
def hanoi(n, source, helper, target):
print ("hanoi( ", n, source, helper, target, " called")
if n > 0:
hanoi(n - 1, source, target, helper)
if source[0]:
disk = source[0].pop()
print( "moving " + str(disk) + " from " + source[1] + " to " + target[1])
target[0].append(disk)
hanoi(n - 1, helper, source, target)
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.