fork download
  1. def tower_of_hanoi(n, source, destination, helper):
  2. if n==1:
  3. print ("Move disk 1 from peg", source," to peg", destination)
  4. return
  5. tower_of_hanoi(n-1, source, helper, destination)
  6. print ("Move disk",n," from peg", source," to peg", destination)
  7. tower_of_hanoi(n-1, helper, destination, source)
  8. # n = number of disks
  9. n = 3
  10. tower_of_hanoi(n,' A','B','C')# your code goes here
Success #stdin #stdout 0.01s 7260KB
stdin
Standard input is empty
stdout
('Move disk 1 from peg', ' A', ' to peg', 'B')
('Move disk', 2, ' from peg', ' A', ' to peg', 'C')
('Move disk 1 from peg', 'B', ' to peg', 'C')
('Move disk', 3, ' from peg', ' A', ' to peg', 'B')
('Move disk 1 from peg', 'C', ' to peg', ' A')
('Move disk', 2, ' from peg', 'C', ' to peg', 'B')
('Move disk 1 from peg', ' A', ' to peg', 'B')