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

Tell us what’s happening:

It says " You should modify your print() call to print A , B , C , instead of the rods object. Keep the newline character in the print() call.'" but I’ve already cheked the variables and i´m not sure of what should I do. I’ve also deleted the rods but what else should I try?

Your code so far


# User Editable Region

NUMBER_OF_DISKS = 4
A = list(range(NUMBER_OF_DISKS, 0, -1))
B = []
C = []


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(A, B, C)
        
        # move the n - 1 disks that we left on auxiliary onto target
        move(n - 1,  auxiliary, source, target)
              
# initiate call from source A to target C with auxiliary B
move(NUMBER_OF_DISKS, A, B, C)

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36

Challenge Information:

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

Hello @AllieN!

You are doing it correctly but your code has some missing points. Because you remove the rods variable, you shouldn’t access the target, and source properties with the rods dictionary because your function already accepts them as parameters and you no longer have the rods variable.

# ❌ Wrong Way
print(someDictionary[someProperty])

# ✅ Correct Way
print(someProperty)

Also, you should add a newline character to the end of the print statement.

print(variable1, variable2, variable3, '\n')

Happy coding!

1 Like

Hello @AllieN

Part of the step it says:

Refactor your code to reflect these changes.

Refactor is coding jargon to mean that the “way” the code does the work is different but the result of the work “must” be the same.

You were asked to change the input structure from a dictionary to single array variables. Therefore, in order to make the result the same you must look what you were doing with the dictionary before

        # move the nth disk from source to target
        rods[target].append(rods[source].pop())

… and, now, you have to do it without the dictionary.
You were appending to a target (found inside the rods dictionary) what you were removing from a source (found inside the rods dictionary).
Use the target and the source alone to do the same.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.