{ "cells": [ { "cell_type": "code", "execution_count": 39, "id": "3c890c42", "metadata": {}, "outputs": [], "source": [ "class VetorNaoOrdenado:\n", " def __init__(self, capacidade):\n", " self.capacidade : int = capacidade\n", " self.ultima_posicao : int = -1\n", " self.valores = [None] * capacidade\n", "\n", " def imprime(self):\n", " print(\"\\n===Vetor===\")\n", " if self.ultima_posicao == -1:\n", " print('O vetor está vazio')\n", " else:\n", " for i in range(self.ultima_posicao + 1):\n", " print(i, ' - ', self.valores[i])\n", "\n", " def insere(self, valor):\n", " if self.ultima_posicao == self.capacidade - 1:\n", " print('Capacidade máxima atingida')\n", " else:\n", " self.ultima_posicao += 1\n", " self.valores[self.ultima_posicao] = valor\n", "\n", " def pesquisar(self, valor):\n", " for i in range(self.ultima_posicao + 1):\n", " if valor == self.valores[i]:\n", " return i\n", " return -1\n", "\n", " def remover(self, valor):\n", " posicao = self.pesquisar(valor);\n", " if posicao == -1:\n", " return -1\n", " else:\n", " for i in range(posicao, self.ultima_posicao):\n", " self.valores[i] = self.valores[i + 1]\n", " self.ultima_posicao -= 1" ] }, { "cell_type": "code", "execution_count": 40, "id": "fb7638ff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "===Vetor===\n", "O vetor está vazio\n" ] } ], "source": [ "vetor = VetorNaoOrdenado(4)\n", "vetor.imprime()" ] }, { "cell_type": "code", "execution_count": 41, "id": "9fb574a0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "===Vetor===\n", "0 - S\n", "1 - A\n", "2 - T\n", "3 - C\n" ] } ], "source": [ "vetor.insere(\"S\")\n", "vetor.insere(\"A\")\n", "vetor.insere(\"T\")\n", "vetor.insere(\"C\")\n", "\n", "vetor.imprime()" ] }, { "cell_type": "code", "execution_count": 42, "id": "af92e5e2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Capacidade máxima atingida\n" ] } ], "source": [ "vetor.insere(\"A\")" ] }, { "cell_type": "code", "execution_count": 43, "id": "08861ae1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vetor.pesquisar(\"C\")" ] }, { "cell_type": "code", "execution_count": 44, "id": "750d4184", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vetor.pesquisar(\"X\")" ] }, { "cell_type": "code", "execution_count": 45, "id": "c3100e92", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vetor.remover(\"X\")" ] }, { "cell_type": "code", "execution_count": 46, "id": "1a8cab2f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "===Vetor===\n", "0 - A\n", "1 - T\n", "2 - C\n" ] } ], "source": [ "vetor.remover(\"S\")\n", "vetor.imprime()" ] }, { "cell_type": "code", "execution_count": 47, "id": "8aa9e5ca", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "===Vetor===\n", "0 - A\n", "1 - C\n" ] } ], "source": [ "vetor.remover(\"T\")\n", "vetor.imprime()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.5" } }, "nbformat": 4, "nbformat_minor": 5 }