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

Tell us what’s happening:

My algorithm works fine (I think), everything seems to be correct, but I have issues with test 4, 5, 6, 7, 8.

Your code so far

def hanoi_solver(number) :
    string = ''
    rod_1 = []
    rod_2 = []
    rod_3 = []
    numbers = range(number, 0, -1)
    for n in numbers :
        rod_1.append(n)

    def move(n, start, help, end) :
        nonlocal string
        if n == 1 :
            end.append(start.pop())
            string += str(rod_1) + str(rod_2) + str(rod_3 )+ '\n'
        else :
            move(n - 1, start, end, help)
            end.append(start.pop())
            string += str(rod_1) + str(rod_2) + str(rod_3 ) + '\n'
            move(n - 1, help, start, end)
    
    string += str(rod_1) + str(rod_2) + str(rod_3 ) + '\n'
    move(number, rod_1, rod_2, rod_3)     
    return string.strip('\n')

test = 4
print(hanoi_solver(test))

Your browser information:

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

Challenge Information:

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

Hi @Jarvis_T and welcome to our community!

If you compare your output with the expected output of the failing tests, do you see a common discrepancy with the formatting?

I understood, there is no space between each printed list, I thought there was. :sweat_smile: Thank you very much for your help @igorgetmeabrain, it was really useful.

1 Like