Advertisement
NosideedisoN

Recursion

Jan 12th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. # Tower of Hanoi

  2. def moveTower(height, fromPole, toPole, withPole):
    
  3.     print('if %s is greater than or equal to 1' %height)
    
  4.     if height >= 1:
        
  5.     print('first call, height is ', height)
        
  6.     moveTower(height - 1, fromPole, withPole, toPole)
        
  7.     print('executing moveDisk with height ', height)
        
  8.     moveDisk(fromPole, toPole)
        
  9.     print('second call, height is ', height)
        
  10.    moveTower(height - 1, withPole, toPole, fromPole)
      
  11.    print('height is ', height)
  12.  
  13. 


def moveDisk(fp, tp):
    
  14.     print('moving disk from', fp, 'to', tp)

print('initial call, height is 3')
  15.  
  16. moveTower(3, 'A', 'B', 'C')
  17.  
  18.  
  19. Print output:
  20. initial call, height is 3
  21. if 3 is greater than or equal to 1
  22. ('first call, height is %s', 3)
  23. if 2 is greater than or equal to 1
  24. ('first call, height is %s', 2)
  25. if 1 is greater than or equal to 1
  26. ('first call, height is %s', 1)
  27. if 0 is greater than or equal to 1
  28.  
  29. executing moveDisk with height 1*
  30. ('moving disk from', 'A', 'to', 'B')
  31.  
  32. ('second call, height is ', 1)
  33. if 0 is greater than or equal to 1
  34. ('height is ', 1)
  35.  
  36. executing moveDisk with height 2*
  37. ('moving disk from', 'A', 'to', 'C')
  38.  
  39. ('second call, height is ', 2)
  40. if 1 is greater than or equal to 1
  41. ('first call, height is %s', 1)
  42. if 0 is greater than or equal to 1
  43.  
  44. executing moveDisk with height 1
  45. ('moving disk from', 'B', 'to', 'C')
  46. —execute as normal
  47. ('second call, height is ', 1)
  48. if 0 is greater than or equal to 1
  49. ('height is ', 1)
  50. ('height is ', 2)
  51.  
  52. executing moveDisk with height 3*
  53. ('moving disk from', 'A', 'to', 'B')
  54. ('second call, height is ', 3)
  55. if 2 is greater than or equal to 1
  56. ('first call, height is %s', 2)
  57. if 1 is greater than or equal to 1
  58. ('first call, height is %s', 1)
  59. if 0 is greater than or equal to 1
  60. executing moveDisk with height 1
  61. ('moving disk from', 'C', 'to', 'A')
  62.  
  63. ('second call, height is ', 1)
  64. if 0 is greater than or equal to 1
  65. ('height is ', 1)
  66. executing moveDisk with height 2
  67. ('moving disk from', 'C', 'to', 'B')
  68.  
  69. ('second call, height is ', 2)
  70. if 1 is greater than or equal to 1
  71. ('first call, height is %s', 1)
  72. if 0 is greater than or equal to 1
  73. executing moveDisk with height 1
  74. ('moving disk from', 'A', 'to', 'B')
  75.  
  76. ('second call, height is ', 1)
  77. if 0 is greater than or equal to 1
  78. ('height is ', 1)
  79. ('height is ', 2)
  80. ('height is ', 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement