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

Tell us what’s happening:

I have coded all the possible moves to be called in a specific sequence, and am completely stuck here. How do I take it from here?

Your code so far

def hanoi_solver(n):
    one = [i for i in range(n, 0, -1)]
    two = []
    three = []
    moves = f'{one} {two} {three}\n'
    i = 50
    while three != list(one) and i > 0:
        if (one and not three) or (one and three[-1] > one[-1]):
            three.append(one.pop())
        elif one and not two or (one and two[-1] > one[-1]):
            two.append(one.pop())
        elif (three and not two) or (three and two[-1] > three[-1]):
            two.append(three.pop())
        elif (two and not one) or (two and one[-1] > two[-1]):
            one.append(two.pop())
        elif (two and not three) or (two and three[-1] > two[-1]):
            three.append(two.pop())
        elif (three and not one) or (three and one[-1] > three[-1]):
            one.append(three.pop())
        else:
            return 'Incorrect solution'
        moves += f'{one} {two} {three}\n'
        i -= 1
    return moves

print(hanoi_solver(3))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0

Challenge Information:

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

I found this one pretty tough, because there are two big steps:

  1. Design the algorithm
  2. implement the algorithm

Can you describe in English or pseudo code the algorithm that you are trying to implement? Do this planning and research first, before you start coding.

I would suggest doing a lot of outside research about the algorithm itself until you have an understanding of it.

Here’s how yours is working:

[3, 2, 1] [] []
[3, 2] [] [1]
[3] [2] [1]
[3] [2, 1] []
[] [2, 1] [3]
[1] [2] [3]
[] [2] [3, 1]

Your algorithm does well here until [] [2] [3, 1] it should have moved 2 instead [1] [] [3, 2]

[2, 1] [] []
[2] [] [1]
[] [2] [1]
[] [2, 1] []

Here as well, it should do this instead:

[2, 1] [] []
[2] [1] []
[] [1] [2]
[] [] [2,1]

Here it should have ended at the second step, right?

[1] [] []
[] [] [1]  #end here
[] [1] []

Make sure your algorithm works for these smaller, simpler values first.

There’s no situation where your program should return 'Incorrect solution'

Again, I suggest a lot of research to understand the problem better. This video really helped me to understand the algorithm

https://www.youtube.com/watch?v=rf6uf3jNjbo

Here’s an explanation with pseudo code: https://expertbeacon.com/how-to-solve-the-tower-of-hanoi-problem-an-illustrated-algorithm-guide/

This also has good information about the algorithm to follow https://www.youtube.com/watch?v=fESwgKNITGE

This one is interesting but I’m not sure how useful it is https://www.youtube.com/watch?v=PGuRmqpr6Oo

Some of these links do have code implementations but they are a bit different than what is required for this challenge.

3 Likes

Had a look at the first link and was easily able to get it done after. While I didn’t have a look at the code I did see the algorithm which I pretty much implemented straight up. Should I attribute this as legitimate research or not?

1 Like

It’s a fair question.

Doing any kind of outside research and studying is not against the rules of freeCodeCamp. You should always be searching for more information.

I think research like this is a given for this problem. I think asking learners to design this algorithm at this stage is too much, but implementing a given algorithm is a fair challenge. I was stuck as well and I never would have completed this without a lot of extra resarch, that’s just part of my problem solving process as well.

I was going to voice the concern that this lab was a bit much, but I wanted to wait and see if more people get stuck here first. This thread is a good data point so thanks for opening it.

2 Likes