Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. class StackIntNumbers(object):
  2.     """ Implement a stack of integers in which the operation of adding, removing, and finding
  3.    the maximum value has a constant runtime.
  4.    """
  5.  
  6.     def __init__(self):
  7.         self.stack = []
  8.         self._max = None
  9.  
  10.     def push(self, number):
  11.         if self._max is None:
  12.             self._max = number
  13.         elif self._max < number:
  14.             self._max = number
  15.         self.stack.append(number)
  16.  
  17.     def pop(self):
  18.         return self.stack.pop()
  19.  
  20.     def top(self):
  21.         if not self.emtpy():
  22.             return self.stack[-1]
  23.  
  24.     def emtpy(self):
  25.         return self.stack == []
  26.  
  27.     def max(self):
  28.         return self._max
  29.  
  30.  
  31. stack = StackIntNumbers()
  32. stack.push(3)
  33. stack.push(10)
  34. stack.push(12)
  35. stack.push(1)
  36.  
  37. assert stack.max() == 12
  38. assert stack.pop() == 1
  39. assert stack.pop() == 12
  40. assert stack.max() == 10  # Fails here. I expect 10 to be max because 12 is out.
  41. assert not stack.emtpy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement