SHARE
TWEET

Untitled

a guest Jan 20th, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Stack:
  2.  
  3.     def __init__(self):
  4.         """
  5.        -------------------------------------------------------
  6.        Initializes an empty stack. Data is stored in a Python list.
  7.        Use: stack = Stack()
  8.        -------------------------------------------------------
  9.        Returns:
  10.            a new Stack object (Stack)
  11.        -------------------------------------------------------
  12.        """
  13.         self._values = []
  14.  
  15.     def push(self, value):
  16.         """
  17.        -------------------------------------------------------
  18.        Pushes a copy of value onto the top of the stack.
  19.        Use: stack.push(value)
  20.        -------------------------------------------------------
  21.        Parameters:
  22.            value - value to be added to stack (?)
  23.        Returns:
  24.            None
  25.        -------------------------------------------------------
  26.        """
  27.         self._values.append(deepcopy(value))
  28.         return
  29.  
  30.     def pop(self):
  31.         """
  32.        -------------------------------------------------------
  33.        Pops and returns the top of stack. The value is removed
  34.        from the stack. Attempting to pop from an empty stack
  35.        throws an exception.
  36.        Use: value = s.pop()
  37.        -------------------------------------------------------
  38.        Returns:
  39.            value - the value at the top of the stack (?)
  40.        -------------------------------------------------------
  41.        """
  42.         assert len(self._values) > 0, "Cannot pop from an empty stack"
  43.         return self._values.pop()
  44.  
  45.         # Your code here
  46.  
  47.     def peek(self):
  48.         """
  49.        -------------------------------------------------------
  50.        Returns a copy of the value at the top of the stack.
  51.        Attempting to peek at an empty stack throws an exception.
  52.        Use: value = s.peek()
  53.        -------------------------------------------------------
  54.        Returns:
  55.            value - a copy of the value at the top of the stack (?)
  56.        -------------------------------------------------------
  57.        """
  58.         new_list = deepcopy(self._values)
  59.  
  60.         assert len(self._values) > 0, "Cannot peek at an empty stack"
  61.         return self._values[len(self._values)-1]
  62.  
  63.         # Your code here
  64.  
  65.  
  66.     def __iter__(self):
  67.         """
  68.        FOR TESTING ONLY
  69.        -------------------------------------------------------
  70.        Generates a Python iterator. Iterates through the stack
  71.        from top to bottom.
  72.        Use: for v in s:
  73.        -------------------------------------------------------
  74.        Returns:
  75.            value - the next value in the stack (?)
  76.        -------------------------------------------------------
  77.        """
  78.         for value in self._values[::-1]:
  79.             yield value
  80.  
  81.  
  82.     def is_empty(self):
  83.         """
  84.        -------------------------------------------------------
  85.        Determines if the stack is empty.
  86.        Use: b = s.is_empty()
  87.        -------------------------------------------------------
  88.        Returns:
  89.            True if the stack is empty, False otherwise
  90.        -------------------------------------------------------
  91.        """
  92.         return self._values == []
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top