Advertisement
elcocodrilotito

Clase Conjunto (en mantenimiento)

Apr 14th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.82 KB | None | 0 0
  1. #Clase Conjunto
  2.  
  3. class Conjunto(object):
  4.     """Como la clase set()"""
  5.  
  6.     #Inicializa un conjunto vacío
  7.     def __init__(self):
  8.         self.__elementos=dict() #Voy a guardar cada elemento del conjunto como clave y valor de un diccionario para minimizar el coste de búsqueda
  9.         self.__tipo_de_elementos=None
  10.        
  11.  
  12.     #Tipo de dato que guarda el conjunto
  13.     def tipo(self):
  14.         if len(self.__elementos)==0:
  15.             self.__tipo_de_elementos=None
  16.         else:
  17.             self.__tipo_de_elementos=type(list(self.__elementos.keys())[0])
  18.  
  19.     def añadir(self,*añadiendo): #pongo *añadiendo por si quieren añadir más de uno a la vez
  20.         for i in añadiendo:
  21.             self.__elementos[i]=i   #si no está, se añade, y si está, se "actualiza", aunque por el mismo elemento
  22.  
  23.         """ El tema es que no sé cuál es más eficiente, el de arriba o este en verde
  24.        for i in añadiendo:
  25.            if i not in self.__elementos:
  26.                self.__elementos[i]=i
  27.        """
  28.  
  29.     def eliminar(self,*eliminando): #pongo *eliminando por si quiero eliminar más de uno a la vez
  30.         for i in eliminando:
  31.             try:                                 #no quiero que me de errores si no está
  32.                 del self.__elementos[eliminando]
  33.             except KeyError:
  34.                 continue
  35.  
  36.  
  37.     def __contains__(self,item):
  38.         if item in self.__elementos:
  39.             return True
  40.         else:
  41.             return False
  42.  
  43.     def __iter__(self):
  44.         return _Conjunto_Iterator(self.__elementos)
  45.  
  46.     def __len__(self):
  47.         return len(self.__elementos)
  48.  
  49.     def pop(self,item):
  50.         x = self.__elementos[item]
  51.         self.eliminar(item)     #Uso mi método
  52.         return x
  53.  
  54.     def __eq__(self,other):
  55.         for i in other:     #Aquí se usará mi iterador
  56.             if i not in self.__elementos:
  57.                 return False
  58.         return True
  59.  
  60.     def vaciar(self):
  61.         self.__elementos.clear()
  62.  
  63.     def copiar(self):
  64.         copia = Conjunto()
  65.         for i in self.__elementos:
  66.             copia.añadir(i)
  67.         return copia
  68.  
  69.     def __or__(self,other): #Unión
  70.         union = Conjunto()
  71.         for i in self:
  72.             union.añadir(i)
  73.         for i in other:
  74.             union.añadir(i)
  75.         return union
  76.  
  77.     def __and__(self,toher): #Intersección
  78.         interseccion = Conjunto()
  79.         if len(self) < len(other): #Esto lo hago para recorrer el conjunto más pequeño solamente
  80.             menor = self
  81.             mayor = other
  82.         else:
  83.             menor = other
  84.             mayor = self
  85.         for i in menor:
  86.             if i in mayor:
  87.                 interseccion.añadir(i)
  88.         return interseccion
  89.  
  90.     def __sub__(self,other): #Diferencia
  91.         diferencia = Conjunto()
  92.         for i in self:
  93.             if i not in other:
  94.                 diferencia.añadir(i)
  95.         return diferencia
  96.  
  97.     def __xor__(self,other): #Diferencia simétrica
  98.         return (self-other)|(other-self)
  99.  
  100.     def es_subconjunto(self,other): #Supongo que se refiere a que a ver si self es subconjunto de other
  101.         for i in self:
  102.             if i not in other:
  103.                 return False
  104.         return True
  105.  
  106.     def es_superconjunto(self,other): #Supongo que se refiere a que a ver si self es superconjunto de other
  107.         for i in other:
  108.             if i not in self:
  109.                 return False
  110.         return True
  111.  
  112.  
  113. class _Conjunto_Iterator:
  114.     def __init__(self,elementos_dict):
  115.         self.__keys = list(elementos_dict.keys())
  116.         self.__index = 0
  117.  
  118.     def __iter__(self):
  119.         return self
  120.  
  121.     def __next__(self):
  122.         if self.__index < len(self.__keys):
  123.             key = self.__keys[self.__index]
  124.             self.__index += 1
  125.         else:
  126.             raise StopIteration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement