fork download
  1. #
  2. class arraystack:
  3. def __init__(self):
  4. self.data=[]
  5. def __len__(self):
  6. return len(self.data)
  7. def is_empty(self):
  8. return len(self.data)==0
  9. def push(self,e):
  10. self.data.append(e)
  11. def top(self):
  12. if self.is_empty():
  13. raise Exception('stack is empty')
  14. else :
  15. return self.data[-1]
  16. def pop(self):
  17. if self.is_empty():
  18. raise Exception('stack is empty')
  19. else:
  20. self.data.pop();
  21.  
  22.  
  23. s = arraystack()
  24. s.push(10)
  25. s.push(20)
  26. print("Top of stack:", s.top()) # المفروض تطبع 20
  27. s.pop()
  28. print("Top after pop:", s.top()) # المفروض تطبع 10
  29. print("Length of stack:", len(s)) # المفروض تطبع 1
  30. print("Is empty?", s.is_empty()) # المفروض تطبع False
  31. s.pop()
  32. print("Is empty after popping all?", s.is_empty()) # المفروض تطبع True
  33.  
Success #stdin #stdout 0.02s 7140KB
stdin
Standard input is empty
stdout
('Top of stack:', 20)
('Top after pop:', 10)
('Length of stack:', 1)
('Is empty?', False)
('Is empty after popping all?', True)