Learn Recursion by Solving the Tower of Hanoi Puzzle - Step 26

Tell us what’s happening:

Im trying to remove the last element by [-1] and adding the target one with append but its not working

Your code so far

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
    for i in range(number_of_moves):
        remainder = (i + 1) % 3

/* User 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}')

/* User 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')

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge Information:

Learn Recursion by Solving the Tower of Hanoi Puzzle - Step 26

Hello Iswoboda,

So in this question we’re going to append to the target rod and pop from the source rod at the same time. So think of it this way.

You have to append something to the target rod, and in the append call you can pop the last item straight away to basically give the last element to the target rod.

Also as a hint, the default value of .pop() is -1. So if you don’t fill in anything it will always pop the last item.


I changed it to this rods[source].append([target], pop()) but it still doesnt work, I thought I had to append pop() as the second argument

You got closer but right now you are trying to append somehing to the source, not to the target rod.
Also inside the append you can just use the .pop() method on the source rod.

I tried thisrods[target].append(pop([source])) but it still doesnt work

Almost there! So now instead of pop([source]) we want to call .pop() on the source rod. (To get the source rod, is the same as you did for the target rod). You don’t have to have anything inside .pop() as the default value is -1 so it will take the last element by default.

1 Like

Im trying this now rods[source].append(pop()) but it doesnt pass

You have to call .pop() on rods[target] since you want to remove the last item from the target rod and give it to the source rod.

I dont really understant rods[target].append(pop()) this still doesnt work, do i need to append rods[source] on a different line perhaps?

No you’re close, but right now you’re popping nothing. The pop method needs to be called on something to be able to remove, and return the last element. so inside the append method, before the pop method you have to add the source rod, so it knows to take that element.

I tried rods[target].append(source[rod].pop()) but it doesnt work

Instead of source[rod] look at how you got the target rod: rods[target].

figured it out thanks

1 Like

Here are a few resources to review .pop():



1 Like

Hey HungryBee, thanks for the hints, I followed this thread to help me resolve. But shouldn’t the code still be valid id we use the -1 value?

It might still be valid Python code, (it has the same effect in the output) but may not pass the tests.

(it would also be .pop(-1) not .pop([-1]))

Generally I would think of pop as popping off the last element, acting like that stack. Optionally, you could pop a different element. This is why I think the tests are looking for just .pop().