project
unknown
python
4 years ago
1.9 kB
6
Indexable
class No: def __init__(self, carga=0, anterior=None): self.carga = carga self.anterior = anterior def __repr__(self): if(self.anterior == None): return '%s' % (self.carga) else: return '%s -> %s' % (self.carga, self.anterior) class Pilha: def __init__(self): self.topo = None def is_empty(self): return self.topo is None def push(self, elemento): no = No(elemento) no.anterior = self.topo self.topo = no def pop(self): assert self.topo != None, "Impossível remover elemento de pilha vazia." elemento_a_remover = self.topo self.topo = self.topo.anterior return elemento_a_remover def __repr__(self): return str(self.topo) def __len__(self): atual = self.topo c = 0 while atual is not None: c += 1 atual = atual.anterior return c def __getitem__(self, indice): if isinstance(indice, slice): fatia = Pilha() atual: 'No' = self.topo inicio = indice.start if indice.start else 0 final = indice.stop if indice.stop else self.total for i in range(final): if i >= inicio: fatia.inserir_valor_no_final(atual.carga) atual = atual.anterior return fatia else: atual: 'No' = self.topo for i in range(indice): atual = atual.anterior return atual.carga from seed import loadComputers, loadResources from pilha import * computadores = loadComputers() recursos = loadResources() comps = Pilha() rec = Pilha() for i in range (len(computadores)): comps.push(computadores[i]) for i in range (len(recursos)): rec.push(recursos[i]) print(comps[0].ip) print(rec[0].name)
Editor is loading...