You will write the recursive solution of the Tower of Hanoi, list its moves, and check them against the rules.
The idea. To move discs from peg a to peg c with spare peg b: move discs from a to b, move the largest disc from a to c, then move the discs from b to c. Induction shows this takes moves.
Your tasks.
moves(n, a, b, c). Return the list of moves, each a pair (from_peg, to_peg), that carries discs from a to c.count(n). Return the number of moves by the recursion , with .legal(n, move_list). Play the moves on three pegs and return True if no disc is ever put on a smaller one and all discs end on peg "C".Run your code once to unlock the worked solution.
Have you tried it?
Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.
Log in to join the discussion.
Loading…