Created
January 7, 2019 13:49
-
-
Save Fhernd/858b3fa102144ca661468cfabcb4518f to your computer and use it in GitHub Desktop.
Creación de contenedor personalizado. Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
import bisect | |
class ElementosOrdenados(collections.Sequence): | |
def __init__(self, inicial = None): | |
self._elementos = sorted(inicial) if inicial is not None else [] | |
def __getitem__(self, indice): | |
return self._elementos[indice] | |
def __len__(self): | |
return len(self._elementos) | |
def agregar(self, elemento): | |
bisect.insort(self._elementos, elemento) | |
elementos = ElementosOrdenados([2, 3, 5]) | |
print(list(elementos)) | |
print(elementos[0]) | |
print(elementos[-1]) | |
elementos.agregar(-2) | |
print(list(elementos)) | |
print(elementos[1:4]) | |
print(5 in elementos) | |
print(len(elementos)) | |
for elmt in elementos: | |
print(elmt, ' ', end='') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment