Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created January 7, 2019 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/858b3fa102144ca661468cfabcb4518f to your computer and use it in GitHub Desktop.
Save Fhernd/858b3fa102144ca661468cfabcb4518f to your computer and use it in GitHub Desktop.
Creación de contenedor personalizado. Python.
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