Back to Freecodecamp

Step 47

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

latest1.1 KB
Original Source

--description--

Before appending the last element to the target, add a comment saying move the nth disk from source to target.

--hints--

You should add a comment with the provided text before rods[target].append(rods[source].pop()).

js
({ test: () => assert.match(code, /move\(\s*n\s*-\s*1\s*,\s*source\s*,\s*auxiliary\s*,\s*target\s*\)\s+#\s*move\sthe\snth\sdisk\sfrom\ssource\sto\starget\s+(?=rods\s*\[\s*target\s*\]\s*\.append\(\s*rods\s*\[\s*source\s*\]\s*\.pop\(\s*\)\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)
        
        rods[target].append(rods[source].pop())
        
        # display starting configuration
        print(rods, '\n')
              
--fcc-editable-region--
# initiate call from source A to target C with auxiliary B
move(NUMBER_OF_DISKS, 'A', 'B', 'C')