fork download
  1. class Conjunto:
  2. """Representa una lista de elementos que no se repiten."""
  3. def __init__(self):
  4. self.__conj = []
  5. ...
  6.  
  7. def __add(self, val):
  8. self.__conj.append(val)
  9. ...
  10.  
  11. def __str__(self):
  12. return str.join(", ", [str(x) for x in self.__conj])
  13. ...
  14.  
  15. def __chk_invariante(self):
  16. for i, x in enumerate(self.__conj):
  17. cdr = self.__conj[i + 1:]
  18. if x in cdr:
  19. raise ValueError(f"{x} repetido en: {cdr.index(x) + i + 1}")
  20. ...
  21. ...
  22. ...
  23.  
  24. def __getattr__(self, str_m):
  25. if str_m == "add":
  26. # Utiliza tuplas para invocar dos fuciones desde la lambda,
  27. # devolviendo el retorno de la primera.
  28. return lambda val: (self.__add(val), self.__chk_invariante())[0]
  29. ...
  30.  
  31. raise AttributeError(f"{self.__class__.__name__}.{str_m}??")
  32. ...
  33. ...
  34.  
  35.  
  36. if __name__ == "__main__":
  37. c1 = Conjunto()
  38. c1.add(1)
  39. c1.add(2)
  40. c1.add(2)
  41. print(c1)
  42. ...
  43.  
Runtime error #stdin #stdout #stderr 0.16s 25892KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 40, in <module>
  File "./prog.py", line 28, in <lambda>
  File "./prog.py", line 19, in __chk_invariante
ValueError: 2 repetido en: 2