Implement the Tower of Hanoi Algorithm - Implement the Tower of Hanoi Algorithm

Tell us what’s happening:

I’ve already implemented the logic for the algorithm, but I can’t figure out what is wrong with the formatting. Can anyone please explain to me what is wrong with the return string?

Your code so far

rod1 = []
rod2=[]
rod3=[]

def hanoi_solver(num_of_disks: int) -> str:
    return_str = ''
    global rod1
    rod1 = [disk for disk in range(num_of_disks, 0, -1)]
    return_str = get_lists_string()
    return_str += move_tower(rod1, rod3, rod2, num_of_disks)


    return_str = return_str.strip()

    return return_str

def move_tower(start, target, auxiliary, num_of_disks)->str:
    if(num_of_disks == 1):
        move_disk(start, target)
        return get_lists_string()

    result = ""
    result += move_tower(start, auxiliary, target, num_of_disks-1)

    move_disk(start, target)
    result += get_lists_string()

    result += move_tower(auxiliary, target, start, num_of_disks-1)
    return result

def move_disk(start, target):
    if(len(start) < 1):
        print("Cannot move disk for an empty rod.")
        return;

    target_disk = start[len(start)-1]
    start.remove(target_disk)
    target.append(target_disk)
    

def get_lists_string()->str:
    return f"{rod1} {rod2} {rod3}\n"


print(hanoi_solver(3))

Your browser information:

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

Challenge Information:

Implement the Tower of Hanoi Algorithm - Implement the Tower of Hanoi Algorithm

  1. hanoi_solver(2) should return [2, 1] [] []\n[2] [1] []\n[] [1] [2]\n [] [] [2, 1] .

to see your output in this format use repr

print(repr(hanoi_solver(2)))

it prints '[2, 1] [] []\n[2] [1] []\n[] [1] [2]\n[] [] [2, 1]'
so not you can confront

actual   '[2, 1] [] []\n[2] [1] []\n[] [1] [2]\n[] [] [2, 1]'
expected '[2, 1] [] []\n[2] [1] []\n[] [1] [2]\n [] [] [2, 1]'

uh, let me check the test itself…

yeah, we going to remove that space, it’s not correct

actual   '[2, 1] [] []\n[2] [1] []\n[] [1] [2]\n[] [] [2, 1]'
expected '[2, 1] [] []\n[2] [1] []\n[] [1] [2]\n[] [] [2, 1]'

weird, it’s identical

ah, here it is

print(repr(hanoi_solver(2)))
print(repr(hanoi_solver(2)))

they do have two different outputs

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

I changed the variables to local variables and it works like a charm. Thanks for taking the time to look into it!