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_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

/* 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}')
            rods[source].pop([-1])
            rods[source].append([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/120.0.0.0 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.

3 Likes

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():

https://www.w3schools.com/python/ref_list_pop.asp

https://www.freecodecamp.org/news/python-pop-how-to-pop-from-a-list-or-an-array-in-python/

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().