Advertisement
giGii

Untitled

Mar 4th, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. class StackMax:
  2.     def __init__(self):
  3.         self.arr = []
  4.  
  5.     def push(self, x):
  6.         self.arr.append(x)
  7.  
  8.     # def get_length(self):
  9.     #     return len(self.arr)
  10.  
  11.     def pop(self):
  12.         if len(self.arr):
  13.             return self.arr.pop()
  14.         return 'error'
  15.  
  16.     def get_max(self):
  17.         if len(self.arr):
  18.             return max(self.arr)
  19.         return 'None'
  20.  
  21.  
  22. n = int(input())
  23. new_stack = StackMax()
  24. for i in range(n):
  25.     operation = input()
  26.     if operation.startswith('get_max'):
  27.         print(new_stack.get_max())
  28.     elif operation.startswith('push'):
  29.         value = operation.split()[-1]
  30.         new_stack.push(value)
  31.     elif operation.startswith('pop'):
  32.         call_back = new_stack.pop()
  33.         if call_back == 'error':
  34.             print(call_back)
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement