class Conjunto:
"""Representa una lista de elementos que no se repiten."""
def __init__ ( self ) :
self .__conj = [ ]
...
def __add( self , val) :
self .__conj.append ( val)
...
def __str__ ( self ) :
return str .join ( ", " , [ str ( x) for x in self .__conj] )
...
def __chk_invariante( self ) :
for i, x in enumerate ( self .__conj) :
cdr = self .__conj[ i + 1 :]
if x in cdr:
raise ValueError ( f"{x} repetido en: {cdr.index(x) + i + 1}" )
...
...
...
def __getattr__ ( self , str_m) :
if str_m == "add" :
# Utiliza tuplas para invocar dos fuciones desde la lambda,
# devolviendo el retorno de la primera.
return lambda val: ( self .__add( val) , self .__chk_invariante( ) ) [ 0 ]
...
raise AttributeError ( f"{self.__class__.__name__}.{str_m}??" )
...
...
if __name__ == "__main__" :
c1 = Conjunto( )
c1.add ( 1 )
c1.add ( 2 )
c1.add ( 2 )
print ( c1)
...
Y2xhc3MgQ29uanVudG86CiAgICAiIiJSZXByZXNlbnRhIHVuYSBsaXN0YSBkZSBlbGVtZW50b3MgcXVlIG5vIHNlIHJlcGl0ZW4uIiIiCiAgICBkZWYgX19pbml0X18oc2VsZik6CiAgICAgICAgc2VsZi5fX2NvbmogPSBbXQogICAgLi4uCiAgICAKICAgIGRlZiBfX2FkZChzZWxmLCB2YWwpOgogICAgICAgIHNlbGYuX19jb25qLmFwcGVuZCh2YWwpCiAgICAuLi4KCiAgICBkZWYgX19zdHJfXyhzZWxmKToKICAgICAgICByZXR1cm4gc3RyLmpvaW4oIiwgIiwgW3N0cih4KSBmb3IgeCBpbiBzZWxmLl9fY29ual0pCiAgICAuLi4KCiAgICBkZWYgX19jaGtfaW52YXJpYW50ZShzZWxmKToKICAgICAgICBmb3IgaSwgeCBpbiBlbnVtZXJhdGUoc2VsZi5fX2NvbmopOgogICAgICAgICAgICBjZHIgPSBzZWxmLl9fY29ualtpICsgMTpdCiAgICAgICAgICAgIGlmIHggaW4gY2RyOgogICAgICAgICAgICAgICAgcmFpc2UgVmFsdWVFcnJvcihmInt4fSByZXBldGlkbyBlb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