A recursive algorithm requires a terminal condition, which is usually the simplest case of an algorithm. For Towers of Hanoi, the easiest case is when there are zero disks to move, do nothing.
In Python, one condition for βfalsityβ is any numeric version of zero, so technically, if someone passed a negative number to your algorithm, it fails, so it would be better to check if ndisks > 0 . This stops the recursion when ndisks == 0.
If to move a positive number (n) of disks, a recursive algorithm:
- Move the n-1 drives from the start of the binding to a different binding.
- Move drive n th from start to finish.
- Move n-1 drives from the βotherβ binding to the end.
The description above describes the rest of your code, with the terminal status being a null drive.
source share