Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Question 1
- """
- -------------------------------------------------------
- Array version of the Stack ADT.
- -------------------------------------------------------
- Author: Kevin Lin
- ID: 180419860
- Email: linx1986@mylaurier.ca
- Section: CP164 Lec:B Lab:3
- __updated__ = "2019-01-21"
- -------------------------------------------------------
- """
- from copy import deepcopy
- class Stack:
- def __init__(self):
- """
- -------------------------------------------------------
- Initializes an is_empty stack. Data is stored in a Python list.
- Use: s = Stack()
- -------------------------------------------------------
- Returns:
- a new Stack object (Stack)
- -------------------------------------------------------
- """
- self._values = []
- def is_empty(self):
- """
- -------------------------------------------------------
- Determines if the stack is empty.
- Use: b = s.is_empty()
- -------------------------------------------------------
- Returns:
- True if the stack is empty, False otherwise
- -------------------------------------------------------
- """
- return self._values == []
- def push(self, value):
- """
- -------------------------------------------------------
- Pushes a copy of value onto the top of the stack.
- Use: s.push(value)
- -------------------------------------------------------
- Parameters:
- value - a data element (?)
- Returns:
- None
- -------------------------------------------------------
- """
- self._values.append(deepcopy(object))
- def pop(self):
- """
- -------------------------------------------------------
- Pops and returns the top of stack. The value is removed
- from the stack. Attempting to pop from an empty stack
- throws an exception.
- Use: value = s.pop()
- -------------------------------------------------------
- Returns:
- value - the value at the top of the stack (?)
- -------------------------------------------------------
- """
- assert len(self._values) > 0, "Cannot pop from an empty stack"
- value = deepcopy(self._values[-1])
- del self._values[-1]
- return value
- def peek(self):
- """
- -------------------------------------------------------
- Returns a copy of the value at the top of the stack.
- Attempting to peek at an empty stack throws an exception.
- Use: value = s.peek()
- -------------------------------------------------------
- Returns:
- value - a copy of the value at the top of the stack (?)
- -------------------------------------------------------
- """
- assert len(self._values) > 0, "Cannot peek at an empty stack"
- value = deepcopy(self._values[-1])
- return value
- def __iter__(self):
- """
- FOR TESTING ONLY
- -------------------------------------------------------
- Generates a Python iterator. Iterates through the stack
- from top to bottom.
- Use: for v in s:
- -------------------------------------------------------
- Returns:
- value - the next value in the stack (?)
- -------------------------------------------------------
- """
- for value in self._values[::-1]:
- yield value
- Question 2
- def array_to_stack(s, a):
- """
- -------------------------------------------------------
- Pushes contents of source onto stack. At finish, source is empty.
- Last value in source is at bottom of stack,
- first value in source is on top of stack.
- Use: array_to_stack(s, a)
- -------------------------------------------------------
- Parameters:
- s - a Stack object (Stack)
- a - a Python list (list)
- Returns:
- None
- -------------------------------------------------------
- """
- for _ in range(len(a)):
- value = a[-1]
- a.pop(-1)
- s.push(value)
- Question 3
- def stack_to_array(stack, target):
- """
- -------------------------------------------------------
- Pops contents of stack into target. At finish, stack is empty.
- Top value of stack is at end of target,
- bottom value of stack is at beginning of target.
- Use: stack_to_array(stack, target)
- -------------------------------------------------------
- Parameters:
- stack - a Stack object (Stack)
- target - a Python list (list)
- Returns:
- None
- -------------------------------------------------------
- """
- while not stack.is_empty():
- value = stack.pop()
- target.insert(0,value)
- Question 4
- def stack_test(source):
- """
- -------------------------------------------------------
- Tests the methods of Stack for empty and
- non-empty stacks using the data in source:
- is_empty, push, pop, peek
- (Testing pop and peek while empty throws exceptions)
- Use: stack_test(source)
- -------------------------------------------------------
- Parameters:
- source - list of data (list of ?)
- Returns:
- None
- -------------------------------------------------------
- """
- s = Stack()
- a = []
- array_to_stack(s, source)
- print("The stack is empty: {}".format(s.is_empty()))
- print("Top of the stack: {}".format(s.peek()))
- print("Pushes 5 into stack: {}".format(s.push(5)))
- print("Pops top element: {}".format(s.pop()))
- print("Top of the stack: {}".format(s.peek()))
- stack_to_array(s, a)
- Question 5
- """
- -----------------------------------------
- question 5
- -----------------------------------------
- Author: Kevin Lin
- ID: 180419860
- Email: linx1986@mylaurier.ca
- ----------------------------------------
- """
- from utilities import stack_test
- from Movie_utilities import read_movies
- source = []
- fv = open("movies.txt", "r")
- source = read_movies(fv)
- stack_test(source)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement