Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. """
  2. functions to run TOAH tours.
  3. """
  4.  
  5.  
  6. # Copyright 2013, 2014, 2017 Gary Baumgartner, Danny Heap, Dustin Wehr,
  7. # Bogdan Simion, Jacqueline Smith, Dan Zingaro
  8. # Distributed under the terms- of the GNU General Public License.
  9. #
  10. # This file is part of Assignment 1, CSC148, Winter 2018.
  11. #
  12. # This is free software: you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation, either version 3 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This file is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with this file. If not, see <http://www.gnu.org/licenses/>.
  24. # Copyright 2013, 2014 Gary Baumgartner, Danny Heap, Dustin Wehr
  25.  
  26.  
  27. # you may want to use time.sleep(delay_between_moves) in your
  28. # solution for 'if __name__ == "__main__":'
  29.  
  30. import time
  31. from toah_model import TOAHModel
  32.  
  33.  
  34. def tour_of_four_stools(model, delay_btw_moves=0.5, animate=False):
  35. """Move a tower of cheeses from the first stool in model to the fourth.
  36.  
  37. @type model: TOAHModel
  38. TOAHModel with tower of cheese on first stool and three empty
  39. stools
  40. @type delay_btw_moves: float
  41. time delay between moves if console_animate is True
  42. @type animate: bool
  43. animate the tour or not
  44. @rtype: None
  45. """
  46. cheeses = model.get_number_of_cheeses()
  47. i = (min(A(cheeses)))[1]
  48. reves_puzzle(model, cheeses, 0, 1, 2, 3)
  49.  
  50.  
  51.  
  52. #helper functions
  53.  
  54. def A(n):
  55. lst = []
  56. m= n - 1
  57. if n == 0:
  58. return (0, 0)
  59. if n == 1:
  60. return (1, 1)
  61. else:
  62. for i in range(1, n):
  63. a = 2 * P(n - i) + 2 ** i - 1
  64. lst.append((a,i))
  65. return lst
  66.  
  67. def P(n):
  68. lst = []
  69. if n == 1:
  70. return 1
  71. else:
  72. for i in range(1, n):
  73. a = recursive_func(n, i)
  74. lst.append(a)
  75. return min(lst)
  76.  
  77. def recursive_func(n, i):
  78. if n < 1:
  79. return 0
  80. if n == 1:
  81. return 1
  82. if n >= 2:
  83. return 2 * P(n-i) + 2 ** i - 1
  84.  
  85. #reves_puzzle: solves problem for 4 stools, uses functio-n hanoi
  86. def reves_puzzle(toahmodel, n, start, aux1, aux2, target):
  87. #base case matches hanoi
  88. t = min(A(n))
  89. print (t)
  90. if n == 1:
  91. hanoi(toahmodel, n, start, aux1, target)
  92. else:
  93. #algorithm from assignment page
  94. reves_puzzle(toahmodel, n-i, start, aux1, target, aux2)
  95. hanoi(toahmodel, i, start, aux1, target)
  96. reves_puzzle(toahmodel, n-i, aux2, start, aux1, target)
  97.  
  98.  
  99.  
  100. #animation version
  101. def reves_A(toahmodel, delay, n, start, aux1, aux2, target):
  102. sq = round((2*n+1)**(1/2.0))
  103. k = n - sq + 1
  104. #base case matches hanoi
  105. if n == 1:
  106. hanoi(toahmodel, n, start, aux1, target)
  107.  
  108.  
  109.  
  110. #hanoi: solves problem for 3 stools
  111. def hanoi(toahmodel, n, start, aux1, target):
  112. #algorithm and base case found on wikipedia: tower of hanoi
  113. if n == 1: #base case
  114. toahmodel.move(start, target)
  115. else:
  116. hanoi(toahmodel, n-1, start, target, aux1)
  117. toahmodel.move(start, target)
  118. hanoi(toahmodel, n-1, aux1, start, target)
  119.  
  120.  
  121. if __name__ == '__main__':
  122. num_cheeses = 6
  123. delay_between_moves = 0.5
  124. console_animate = False
  125.  
  126. # DO NOT MODIFY THE CODE BELOW.
  127. four_stools = TOAHModel(4)
  128. four_stools.fill_first_stool(number_of_cheeses=num_cheeses)
  129.  
  130. tour_of_four_stools(four_stools,
  131. animate=console_animate,
  132. delay_btw_moves=delay_between_moves)
  133.  
  134. print(four_stools.number_of_moves())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement