Back to Freecodecamp

Step 51

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

latest1.1 KB
Original Source

--description--

Above the second move call, add one last comment saying move the n - 1 disks that we left on auxiliary onto target.

--hints--

You should add a comment using the provided text.

js
({ test: () => assert.match(code, /#\s*move\sthe\sn\s-\s1\sdisks\sthat\swe\sleft\son\sauxiliary\sonto\starget\s+(?=move\(\s*n\s*-\s*1\s*,\s*auxiliary\s*,\s*source\s*,\s*target\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, target, auxiliary)
        
        # move the nth disk from source to target
        rods[target].append(rods[source].pop())
        
        # display our progress
        print(rods, '\n')
        
        move(n - 1,  auxiliary, source, target)
              
--fcc-editable-region--
# initiate call from source A to target C with auxiliary B
move(NUMBER_OF_DISKS, 'A', 'B', 'C')