| about | help | code help+videos | done | prefs |
The Towers of Hanoi is a puzzle where there is a pyramid of disks, each one smaller than the one below it, all placed on one of three rods. The puzzle is to move all the disks from the left rod to the right rod, possibly using the middle rod as necessary. You can only move one disk at a time, onto another rod, but never moving a disk onto a disk that is smaller. For this problem, the list of disks will be represented as a string, like '1234'. This means a pyramid of four disks, with the smallest, '1', on top, and the largest, '4' on the bottom. The rod names are 'L' for left, 'R' for right, and 'M' for middle. The function call move_tower('123', 'L', 'R', 'M') means to move the pile of four disks from the L rod to the R rod, using the M rod if necessary. The function should return a list of the moves required, where each move is represented as a string of the form '1LM', which means to move disk number 1 from the left to the middle rod. move_tower('1', 'L', 'R', 'M') → ['1LR'] move_tower('12', 'L', 'R', 'M') → ['1LM', '2LR', '1MR'] move_tower('123', 'L', 'R', 'M') → ['1LR', '2LM', '1RM', '3LR', '1ML', '2MR', '1LR'] ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 400 Post-solution available
Copyright Nick Parlante 2017 - privacy