Classe em Python - Exemplo de uso com vetores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Criado por: profa. Divani Barbosa Gavinier | |
# Curriculo Lattes: http://lattes.cnpq.br/8503400830635447 | |
# divanibarbosa@gmail.com | |
class Vetor: | |
def __init__(self): | |
self.vetor = [] | |
self.n = 0 | |
def inserir(self,valor): | |
self.vetor.append(valor) | |
self.n = self.n + 1 | |
def mostrar(self): | |
print(end = "[") | |
for i in range(0,self.n,1): | |
print(self.vetor[i], end = " ") | |
print(end = "]") | |
return "" | |
def buscalinear(self,chave): | |
for i in range(0,self.n,1): | |
if self.vetor[i] == chave: | |
return i | |
return -1 | |
def remover(self,chave): | |
posicao = self.buscalinear(chave) | |
if posicao == -1: | |
print("Atenção", chave, "não encontrado para remoção") | |
return | |
self.vetor.pop(posicao) | |
self.n = self.n - 1 | |
# inicio programa principal | |
x = Vetor() | |
x.inserir(8) | |
x.inserir(22) | |
x.inserir(-1) | |
x.inserir(5) | |
x.inserir(30) | |
print("Conteudo:",end=" ") | |
print(x.mostrar()) | |
chave=2 | |
print("Buscando item ",chave,":") | |
if x.buscalinear(chave)==-1: | |
print("Item ",chave," não encontrado") | |
else: | |
print("Item encontrado") | |
chave = 22 | |
print("Removendo item ",chave,": ") | |
x.remover(chave) | |
print("Conteudo:",end=" ") | |
print(x.mostrar()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment