Advertisement
Guest User

Lab2

a guest
Jan 23rd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. Question 1
  2. """
  3. -------------------------------------------------------
  4. Array version of the Stack ADT.
  5. -------------------------------------------------------
  6. Author: Kevin Lin
  7. ID: 180419860
  8. Email: linx1986@mylaurier.ca
  9. Section: CP164 Lec:B Lab:3
  10. __updated__ = "2019-01-21"
  11. -------------------------------------------------------
  12. """
  13. from copy import deepcopy
  14.  
  15. class Stack:
  16.  
  17. def __init__(self):
  18. """
  19. -------------------------------------------------------
  20. Initializes an is_empty stack. Data is stored in a Python list.
  21. Use: s = Stack()
  22. -------------------------------------------------------
  23. Returns:
  24. a new Stack object (Stack)
  25. -------------------------------------------------------
  26. """
  27. self._values = []
  28.  
  29. def is_empty(self):
  30. """
  31. -------------------------------------------------------
  32. Determines if the stack is empty.
  33. Use: b = s.is_empty()
  34. -------------------------------------------------------
  35. Returns:
  36. True if the stack is empty, False otherwise
  37. -------------------------------------------------------
  38. """
  39.  
  40. return self._values == []
  41.  
  42. def push(self, value):
  43. """
  44. -------------------------------------------------------
  45. Pushes a copy of value onto the top of the stack.
  46. Use: s.push(value)
  47. -------------------------------------------------------
  48. Parameters:
  49. value - a data element (?)
  50. Returns:
  51. None
  52. -------------------------------------------------------
  53. """
  54.  
  55. self._values.append(deepcopy(object))
  56.  
  57. def pop(self):
  58. """
  59. -------------------------------------------------------
  60. Pops and returns the top of stack. The value is removed
  61. from the stack. Attempting to pop from an empty stack
  62. throws an exception.
  63. Use: value = s.pop()
  64. -------------------------------------------------------
  65. Returns:
  66. value - the value at the top of the stack (?)
  67. -------------------------------------------------------
  68. """
  69. assert len(self._values) > 0, "Cannot pop from an empty stack"
  70.  
  71. value = deepcopy(self._values[-1])
  72. del self._values[-1]
  73.  
  74. return value
  75.  
  76. def peek(self):
  77. """
  78. -------------------------------------------------------
  79. Returns a copy of the value at the top of the stack.
  80. Attempting to peek at an empty stack throws an exception.
  81. Use: value = s.peek()
  82. -------------------------------------------------------
  83. Returns:
  84. value - a copy of the value at the top of the stack (?)
  85. -------------------------------------------------------
  86. """
  87. assert len(self._values) > 0, "Cannot peek at an empty stack"
  88.  
  89. value = deepcopy(self._values[-1])
  90.  
  91. return value
  92.  
  93.  
  94. def __iter__(self):
  95. """
  96. FOR TESTING ONLY
  97. -------------------------------------------------------
  98. Generates a Python iterator. Iterates through the stack
  99. from top to bottom.
  100. Use: for v in s:
  101. -------------------------------------------------------
  102. Returns:
  103. value - the next value in the stack (?)
  104. -------------------------------------------------------
  105. """
  106. for value in self._values[::-1]:
  107. yield value
  108.  
  109. Question 2
  110. def array_to_stack(s, a):
  111. """
  112. -------------------------------------------------------
  113. Pushes contents of source onto stack. At finish, source is empty.
  114. Last value in source is at bottom of stack,
  115. first value in source is on top of stack.
  116. Use: array_to_stack(s, a)
  117. -------------------------------------------------------
  118. Parameters:
  119. s - a Stack object (Stack)
  120. a - a Python list (list)
  121. Returns:
  122. None
  123. -------------------------------------------------------
  124. """
  125. for _ in range(len(a)):
  126. value = a[-1]
  127. a.pop(-1)
  128. s.push(value)
  129.  
  130. Question 3
  131. def stack_to_array(stack, target):
  132. """
  133. -------------------------------------------------------
  134. Pops contents of stack into target. At finish, stack is empty.
  135. Top value of stack is at end of target,
  136. bottom value of stack is at beginning of target.
  137. Use: stack_to_array(stack, target)
  138. -------------------------------------------------------
  139. Parameters:
  140. stack - a Stack object (Stack)
  141. target - a Python list (list)
  142. Returns:
  143. None
  144. -------------------------------------------------------
  145. """
  146. while not stack.is_empty():
  147. value = stack.pop()
  148. target.insert(0,value)
  149.  
  150. Question 4
  151. def stack_test(source):
  152. """
  153. -------------------------------------------------------
  154. Tests the methods of Stack for empty and
  155. non-empty stacks using the data in source:
  156. is_empty, push, pop, peek
  157. (Testing pop and peek while empty throws exceptions)
  158. Use: stack_test(source)
  159. -------------------------------------------------------
  160. Parameters:
  161. source - list of data (list of ?)
  162. Returns:
  163. None
  164. -------------------------------------------------------
  165. """
  166. s = Stack()
  167. a = []
  168. array_to_stack(s, source)
  169. print("The stack is empty: {}".format(s.is_empty()))
  170. print("Top of the stack: {}".format(s.peek()))
  171. print("Pushes 5 into stack: {}".format(s.push(5)))
  172. print("Pops top element: {}".format(s.pop()))
  173. print("Top of the stack: {}".format(s.peek()))
  174.  
  175. stack_to_array(s, a)
  176.  
  177. Question 5
  178. """
  179. -----------------------------------------
  180. question 5
  181. -----------------------------------------
  182. Author: Kevin Lin
  183. ID: 180419860
  184. Email: linx1986@mylaurier.ca
  185. ----------------------------------------
  186. """
  187. from utilities import stack_test
  188. from Movie_utilities import read_movies
  189.  
  190.  
  191. source = []
  192. fv = open("movies.txt", "r")
  193.  
  194. source = read_movies(fv)
  195.  
  196. stack_test(source)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement