Back to Freecodecamp

Step 49

curriculum/challenges/english/blocks/learn-recursion-by-solving-the-tower-of-hanoi-puzzle/64df47b32b92301a815d5ef8.md

latest1.3 KB
Original Source

--description--

At first, the recursive call you have just added deals with the sub-problem of moving n - 1 disks to the second rod.

For that reason, the target argument corresponds to your second rod, while the auxiliary argument is the third rod. Keep in mind that those will keep swapping as the recursion proceeds.

Fix the arguments order exchanging target and auxiliary in your recursive call.

--hints--

You should modify the order of the arguments in your move(n - 1, source, auxiliary, target) call.

js
({ test:  () => assert.match(code, /move\(\s*n\s*-\s*1\s*,\s*source\s*,\s*target\s*,\s*auxiliary\s*\)/) })

--seed--

--seed-contents--

py
NUMBER_OF_DISKS = 4
rods = {
    'A': list(range(NUMBER_OF_DISKS, 0, -1)),
    'B': [],
    'C': []
}

--fcc-editable-region--
def move(n, source, auxiliary, target):
    if n > 0:
        # move n - 1 disks from source to auxiliary, so they are out of the way
        move(n - 1, source, auxiliary, target)
        
        # move the nth disk from source to target
        rods[target].append(rods[source].pop())
        
        # display our progress
        print(rods, '\n')
              
--fcc-editable-region--
# initiate call from source A to target C with auxiliary B
move(NUMBER_OF_DISKS, 'A', 'B', 'C')