← Back to challenges

Tower of Hanoi

PythonHardalgorithmsmathnumbersgamesinterview

Instructions

There are three towers. The objective of the game is to move all the disks over to tower #3, but you can't place a larger disk onto a smaller disk.

Tower of Hanoi

Create a function that takes a number discs as an argument and returns the minimum amount of steps needed to complete the game.

Examples

tower_hanoi(3) ➞ 7

tower_hanoi(5) ➞ 31

tower_hanoi(0) ➞ 0

Notes

  • The amount of discs is always a positive integer.
  • 1 disc can be changed per move.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Recursion: Count Vowels