Exercise: Tower of Hanoi

Entertain yourself by solving this fun puzzle.

We'll cover the following

Problem

You're required to implement the solution to the Tower of Hanoi problem, where you are given three pegs, and a stack of disks on one of the pegs such that the disks are in order of increasing diameters from top to bottom. The goal is to move all the disks from peg A to peg C. The disks can only be moved one at a time, from one peg to another, subject to the following two constraints:

  1. Only the top disk in a stack (on a peg) can be moved to a different peg.

  2. When moving a disk, it cannot be placed on another disk that has a smaller diameter.

Get hands-on with 1400+ tech skills courses.