# Lista
class Lista:
class Nodo:
def __init__(self, valor, sig):
self.valor = valor
self.sig = sig
@property
def valor(self):
return self.__valor
@valor.setter
def valor(self, x):
self.__valor = x
@property
def sig(self):
return self.__sig
@sig.setter
def sig(self, x):
self.__sig = x
def __init__(self):
self.__org = None
self.__fin = None
self.__num = 0
@property
def org(self):
return self.__org
@property
def fin(self):
return self.__fin
def inserta(self, x):
assert(x != None)
assert((self.org != None and self.fin != None)
or (self.org == None and self.fin == None))
nodo = Lista.Nodo(x, None)
if self.fin != None:
self.fin.sig = nodo
self.__fin = self.fin.sig
else:
self.__fin = nodo
if self.org == None:
self.__org = self.fin
self.__num += 1
self.__recorre()
if __debug__:
def __recorre(self):
num = 0
it = self.org
while it != None:
num += 1
it = it.sig
print("Recorrido.")
assert(len(self) == num)
else:
def __recorre(self):
pass
def __nonzero__(self):
return len(self) > 0
def __len__(self):
assert(self.__num > 0)
return self.__num
def __str__(self):
toret = "[ "
it = self.org
while it != None:
toret += str(it.valor) + ' '
it = it.sig
return toret + ']'
l = Lista()
l.inserta(1)
l.inserta(2)
l.inserta(3)
print(l)
IyBMaXN0YQpjbGFzcyBMaXN0YToKCQoJY2xhc3MgTm9kbzoKCQlkZWYgX19pbml0X18oc2VsZiwgdmFsb3IsIHNpZyk6CgkJCXNlbGYudmFsb3IgPSB2YWxvcgoJCQlzZWxmLnNpZyA9IHNpZwoJCQkKCQlAcHJvcGVydHkKCQlkZWYgdmFsb3Ioc2VsZik6CgkJCXJldHVybiBzZWxmLl9fdmFsb3IKCQkJCgkJQHZhbG9yLnNldHRlcgoJCWRlZiB2YWxvcihzZWxmLCB4KToKCQkJc2VsZi5fX3ZhbG9yID0geAoJCQkKCQlAcHJvcGVydHkKCQlkZWYgc2lnKHNlbGYpOgoJCQlyZXR1cm4gc2VsZi5fX3NpZwoJCQkKCQlAc2lnLnNldHRlcgoJCWRlZiBzaWcoc2VsZiwgeCk6CgkJCXNlbGYuX19zaWcgPSB4CgkKCWRlZiBfX2luaXRf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