Back to Freecodecamp

Step 26

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

latest1.6 KB
Original Source

--description--

After printing the move, you need to remove the last element from the source rod and append it to target rod. Use the .pop() method and the .append() method for that.

--hints--

You should remove the last element from rods[source].

js
({ test: () => assert.match(code, /rods\s*\[\s*source\s*\]\s*\.pop\s*\(\s*\)/) })

You should have rods[target].append(rods[source].pop()) in your code.

js
({ test: () => assert.match(code, /rods\s*\[\s*target\s*\]\s*\.append\s*\(\s*rods\s*\[\s*source\s*\]\s*\.pop\s*\(\s*\)\s*\)/) })

--seed--

--seed-contents--

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

def move(n, source, auxiliary, target):
    # display starting configuration
    print(rods)
    for i in range(number_of_moves):
        remainder = (i + 1) % 3
--fcc-editable-region--
        if remainder == 1:
            print(f'Move {i + 1} allowed between {source} and {target}')
            forward = False
            if not rods[target]:
                forward = True
            elif rods[source] and rods[source][-1] < rods[target][-1]:
                forward = True
            if forward:
                print(f'Moving disk {rods[source][-1]} from {source} to {target}')
--fcc-editable-region--
        elif remainder == 2:
            print(f'Move {i + 1} allowed between {source} and {auxiliary}')
        elif remainder == 0:
            print(f'Move {i + 1} allowed between {auxiliary} and {target}')

# initiate call from source A to target C with auxiliary B
move(NUMBER_OF_DISKS, 'A', 'B', 'C')