Advertisement
Fhernd

contenedor_personalizado.py

Jan 7th, 2019
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. import collections
  2. import bisect
  3.  
  4.  
  5. class ElementosOrdenados(collections.Sequence):
  6.     def __init__(self, inicial = None):
  7.         self._elementos = sorted(inicial) if inicial is not None else []
  8.  
  9.     def __getitem__(self, indice):
  10.         return self._elementos[indice]
  11.  
  12.     def __len__(self):
  13.         return len(self._elementos)
  14.  
  15.     def agregar(self, elemento):
  16.         bisect.insort(self._elementos, elemento)
  17.  
  18.  
  19. elementos = ElementosOrdenados([2, 3, 5])
  20.  
  21. print(list(elementos))
  22. print(elementos[0])
  23. print(elementos[-1])
  24.  
  25. elementos.agregar(-2)
  26.  
  27. print(list(elementos))
  28. print(elementos[1:4])
  29. print(5 in elementos)
  30. print(len(elementos))
  31.  
  32. for elmt in elementos:
  33.     print(elmt, ' ', end='')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement