Tower of Hanoi

The Tower of Hanoi is a classic puzzle in which disks are moved from one peg to another, without ever placing a larger disk on top of a smaller one.

Problem

The Tower of Hanoi is a classic puzzle with a simple rule: move all the disks from one peg to another, without ever placing a larger disk on top of a smaller one.
Example input:
3 disks
Example output:
1 -> 2
1 -> 3
2 -> 3

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
# The optimal solution is to move the top disk to the middle peg, then move the bottom disk to the last peg, then move the top disk to the last peg.
# This is optimal because it is the minimum number of moves required to move all the disks.

def hanoi(n, start, end, temp):
    if n == 1:
        print(start, "->", end)
    else:
        hanoi(n-1, start, temp, end)
        print(start, "->", end)
        hanoi(n-1, temp, end, start)

hanoi(3, 1, 3, 2)

A.I. Evaluation of the Solution

The candidate's solution is correct and solves the problem. The candidate has taken a recursive approach to solving the problem, which is a good approach. The candidate could improve their solution by adding comments to explain the code.

Evaluated at: 2022-11-14 00:15:56