Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Stack:
- def __init__(self):
- """
- -------------------------------------------------------
- Initializes an empty stack. Data is stored in a Python list.
- Use: stack = Stack()
- -------------------------------------------------------
- Returns:
- a new Stack object (Stack)
- -------------------------------------------------------
- """
- self._values = []
- def push(self, value):
- """
- -------------------------------------------------------
- Pushes a copy of value onto the top of the stack.
- Use: stack.push(value)
- -------------------------------------------------------
- Parameters:
- value - value to be added to stack (?)
- Returns:
- None
- -------------------------------------------------------
- """
- self._values.append(deepcopy(value))
- return
- 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"
- return self._values.pop()
- # Your code here
- 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 (?)
- -------------------------------------------------------
- """
- new_list = deepcopy(self._values)
- assert len(self._values) > 0, "Cannot peek at an empty stack"
- return self._values[len(self._values)-1]
- # Your code here
- 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
- 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 == []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement