Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Clase Conjunto
- class Conjunto(object):
- """Como la clase set()"""
- #Inicializa un conjunto vacío
- def __init__(self):
- self.__elementos=dict() #Voy a guardar cada elemento del conjunto como clave y valor de un diccionario para minimizar el coste de búsqueda
- self.__tipo_de_elementos=None
- #Tipo de dato que guarda el conjunto
- def tipo(self):
- if len(self.__elementos)==0:
- self.__tipo_de_elementos=None
- else:
- self.__tipo_de_elementos=type(list(self.__elementos.keys())[0])
- def añadir(self,*añadiendo): #pongo *añadiendo por si quieren añadir más de uno a la vez
- for i in añadiendo:
- self.__elementos[i]=i #si no está, se añade, y si está, se "actualiza", aunque por el mismo elemento
- """ El tema es que no sé cuál es más eficiente, el de arriba o este en verde
- for i in añadiendo:
- if i not in self.__elementos:
- self.__elementos[i]=i
- """
- def eliminar(self,*eliminando): #pongo *eliminando por si quiero eliminar más de uno a la vez
- for i in eliminando:
- try: #no quiero que me de errores si no está
- del self.__elementos[eliminando]
- except KeyError:
- continue
- def __contains__(self,item):
- if item in self.__elementos:
- return True
- else:
- return False
- def __iter__(self):
- return _Conjunto_Iterator(self.__elementos)
- def __len__(self):
- return len(self.__elementos)
- def pop(self,item):
- x = self.__elementos[item]
- self.eliminar(item) #Uso mi método
- return x
- def __eq__(self,other):
- for i in other: #Aquí se usará mi iterador
- if i not in self.__elementos:
- return False
- return True
- def vaciar(self):
- self.__elementos.clear()
- def copiar(self):
- copia = Conjunto()
- for i in self.__elementos:
- copia.añadir(i)
- return copia
- def __or__(self,other): #Unión
- union = Conjunto()
- for i in self:
- union.añadir(i)
- for i in other:
- union.añadir(i)
- return union
- def __and__(self,toher): #Intersección
- interseccion = Conjunto()
- if len(self) < len(other): #Esto lo hago para recorrer el conjunto más pequeño solamente
- menor = self
- mayor = other
- else:
- menor = other
- mayor = self
- for i in menor:
- if i in mayor:
- interseccion.añadir(i)
- return interseccion
- def __sub__(self,other): #Diferencia
- diferencia = Conjunto()
- for i in self:
- if i not in other:
- diferencia.añadir(i)
- return diferencia
- def __xor__(self,other): #Diferencia simétrica
- return (self-other)|(other-self)
- def es_subconjunto(self,other): #Supongo que se refiere a que a ver si self es subconjunto de other
- for i in self:
- if i not in other:
- return False
- return True
- def es_superconjunto(self,other): #Supongo que se refiere a que a ver si self es superconjunto de other
- for i in other:
- if i not in self:
- return False
- return True
- class _Conjunto_Iterator:
- def __init__(self,elementos_dict):
- self.__keys = list(elementos_dict.keys())
- self.__index = 0
- def __iter__(self):
- return self
- def __next__(self):
- if self.__index < len(self.__keys):
- key = self.__keys[self.__index]
- self.__index += 1
- else:
- raise StopIteration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement