{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "5XmLJ89QA5qC" }, "source": [ "# Estructuras de datos: pilas y colas" ], "id": "5XmLJ89QA5qC" }, { "cell_type": "markdown", "metadata": { "id": "Qv4ReQBnA5qF" }, "source": [ "# Pilas\n", "Método LIFO (Last in First out) \n", "* PUSH: agrega un elemento a la colección\n", "* POP: elimina el elemento agregado más recientemente \n", "\n", "Ejemplo:\n", "- Una pila de libros puestos en vertical sobre una mesa \n", "\n", "LIFO | PILA\n", ":-: | :-:\n", "![pila](https://github.com/financieras/pyCourse/blob/main/jupyter/img/stack.png?raw=1) | ![libros](https://github.com/financieras/pyCourse/blob/main/jupyter/img/pila.jpg?raw=1)" ], "id": "Qv4ReQBnA5qF" }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "qIzwjLkXA5qG" }, "outputs": [], "source": [ "# Se trabaja con listas.\n", "libros = [\"Quijote\", \"Celestina\", \"Principito\"]\n", "# si fuera una pila de libros puestos en vertical, abajo estaría el Quijote y arriba el Principito" ], "id": "qIzwjLkXA5qG" }, { "cell_type": "markdown", "metadata": { "id": "yeV-b_ImA5qH" }, "source": [ "## Añadiendo elementos\n", "Se añaden por la **derecha** con *append*." ], "id": "yeV-b_ImA5qH" }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "rQM4cna9A5qH", "outputId": "fe31c9be-f302-4031-f42a-56da4fc72b25", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Quijote', 'Celestina', 'Principito', 'La Odisea']" ] }, "metadata": {}, "execution_count": 2 } ], "source": [ "libros.append(\"La Odisea\")\n", "libros" ], "id": "rQM4cna9A5qH" }, { "cell_type": "markdown", "metadata": { "id": "HDj4R7yPA5qI" }, "source": [ "## Eliminando elementos\n", "Se eliminan por la **derecha** con *pop*.\n", "El último en llegar es el primero en salir." ], "id": "HDj4R7yPA5qI" }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "hAvBhw9IA5qI", "outputId": "715e7a21-6abe-4de6-b6e7-f87b9c9eb0f8", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'La Odisea'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 3 } ], "source": [ "libros.pop() # devuelve el último elemento de la lista y lo elimina" ], "id": "hAvBhw9IA5qI" }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "sKs-xoSbA5qJ", "outputId": "35fa5df1-5309-4ccc-c80d-fd56fa7101d2", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Quijote', 'Celestina', 'Principito']" ] }, "metadata": {}, "execution_count": 4 } ], "source": [ "libros # observamos que 'La Odisea' ha sido eliminada de la lista" ], "id": "sKs-xoSbA5qJ" }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "YpOjZLpTA5qJ", "outputId": "c2f5bcee-6bc9-4a1f-93c1-3fba9b3e526a", "colab": { "base_uri": "https://localhost:8080/", "height": 53 } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['Quijote', 'Celestina']\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "'Principito'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 5 } ], "source": [ "eliminado = libros.pop() # podemos guardar en una variable el elemento eliminado\n", "print(libros)\n", "eliminado" ], "id": "YpOjZLpTA5qJ" }, { "cell_type": "markdown", "metadata": { "id": "terETtlxA5qK" }, "source": [ "# Colas\n", "Método FIFO (First in First out) \n", "Ejemplo:\n", "- La cola de un establecimiento \n", "\n", "FIFO | QUEUE\n", ":-: | :-:\n", "![McAuto](https://github.com/financieras/pyCourse/blob/main/jupyter/img/cola.jpg?raw=1) | ![queue)](https://github.com/financieras/pyCourse/blob/main/jupyter/img/queue.png?raw=1)" ], "id": "terETtlxA5qK" }, { "cell_type": "markdown", "metadata": { "id": "UaaAj8lZA5qK" }, "source": [ "## Crear una cola\n", "Usamos *deque* de la librería *collections*." ], "id": "UaaAj8lZA5qK" }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "HsxCv8kIA5qK", "outputId": "3c5f003c-b279-4b1a-e337-09c87167a8a3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "deque([])" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "from collections import deque\n", "cola = deque() # creando una cola vacía\n", "cola" ], "id": "HsxCv8kIA5qK" }, { "cell_type": "markdown", "metadata": { "id": "Na7YiRukA5qL" }, "source": [ "## Añadir elementos a la cola\n", "Para añadir elementos a una cola podemos pasar una **lista** al objeto deque." ], "id": "Na7YiRukA5qL" }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "-bBK4TAZA5qL" }, "outputs": [], "source": [ "cola = deque(['Ana', 'Jose', 'Eva']) # Primero llegó Ana a la cola" ], "id": "-bBK4TAZA5qL" }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "_SJtiiYyA5qL", "outputId": "44a8fe9f-6161-4176-edbd-1185ef9da45b", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "deque(['Ana', 'Jose', 'Eva', 'Luis', 'Bea'])" ] }, "metadata": {}, "execution_count": 8 } ], "source": [ "cola.append('Luis')\n", "cola.append('Bea') # Bea es la última en llegar\n", "cola" ], "id": "_SJtiiYyA5qL" }, { "cell_type": "markdown", "metadata": { "id": "Kl0S-3svA5qL" }, "source": [ "## Sacar elementos de la cola\n", "El primer usuario al que atenderemos es al primero que llegó. \n", "Los elementos se sacan por la **izquierda** con *popleft*. \n", "\n", "El método *popleft* no funciona en las listas solo en las queue." ], "id": "Kl0S-3svA5qL" }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "QaOkHgUzA5qM", "outputId": "1d0bf304-540d-455e-a658-fb154c819022", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Ana'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 9 } ], "source": [ "cola.popleft() # Atendemos a Ana" ], "id": "QaOkHgUzA5qM" }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "ObZiXL_0A5qM", "outputId": "feb45aa1-4a25-462c-db6f-0a09fc54d2c3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "deque(['Jose', 'Eva', 'Luis', 'Bea'])" ] }, "metadata": {}, "execution_count": 10 } ], "source": [ "cola" ], "id": "ObZiXL_0A5qM" }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "SEGE3ZMZA5qM", "outputId": "524fc92c-e84e-4196-b237-d5478d4c7ae5", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Jose'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 11 } ], "source": [ "atendido = cola.popleft() # Podemos guardar en una variable los elementos eliminados\n", "atendido" ], "id": "SEGE3ZMZA5qM" }, { "cell_type": "code", "execution_count": 12, "metadata": { "id": "p2eJcUX2A5qM", "outputId": "cfd476fa-a244-4bee-96e3-7aedba6c7334", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "deque(['Eva', 'Luis', 'Bea'])" ] }, "metadata": {}, "execution_count": 12 } ], "source": [ "cola" ], "id": "p2eJcUX2A5qM" }, { "cell_type": "markdown", "metadata": { "id": "00Tp2OiJA5qN" }, "source": [ "### Ejercicio\n", "Nos proporcionan una lista con las fases para la creación de un proyecto tecnológico." ], "id": "00Tp2OiJA5qN" }, { "cell_type": "code", "execution_count": 13, "metadata": { "id": "BMIXLj4uA5qN" }, "outputs": [], "source": [ "fases = ['Identificación','Exploración','Diseño','Planificación','Construcción','Evaluación','Divulgación']" ], "id": "BMIXLj4uA5qN" }, { "cell_type": "markdown", "metadata": { "id": "ErB8JZ_RA5qN" }, "source": [ "Nos piden que asociemos cada fase con su número y las incorporemos a una lista para formar una matriz 7 x 2." ], "id": "ErB8JZ_RA5qN" }, { "cell_type": "code", "execution_count": 14, "metadata": { "id": "QGxqMRonA5qN", "outputId": "956f7a38-b4d5-4050-ec1b-30fe30c0b5c9", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[[1, 'Identificación'],\n", " [2, 'Exploración'],\n", " [3, 'Diseño'],\n", " [4, 'Planificación'],\n", " [5, 'Construcción'],\n", " [6, 'Evaluación'],\n", " [7, 'Divulgación']]" ] }, "metadata": {}, "execution_count": 14 } ], "source": [ "l=[[i+1,fases[i]]for i in range(len(fases))]\n", "l" ], "id": "QGxqMRonA5qN" }, { "cell_type": "markdown", "metadata": { "id": "7Zy3ALyNA5qN" }, "source": [ "Desordenamos las fases." ], "id": "7Zy3ALyNA5qN" }, { "cell_type": "code", "execution_count": 15, "metadata": { "id": "DAz6xutiA5qN", "outputId": "6818dd16-5e4e-4b54-8402-25c9e1d9d2ac", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[[3, 'Diseño'],\n", " [1, 'Identificación'],\n", " [5, 'Construcción'],\n", " [6, 'Evaluación'],\n", " [7, 'Divulgación'],\n", " [4, 'Planificación'],\n", " [2, 'Exploración']]" ] }, "metadata": {}, "execution_count": 15 } ], "source": [ "from random import shuffle\n", "shuffle(l)\n", "l" ], "id": "DAz6xutiA5qN" }, { "cell_type": "markdown", "metadata": { "id": "JPOdokCXA5qO" }, "source": [ "Se pide: \n", " usando colas, mostrar el listado de las fases ordenadas y sin número." ], "id": "JPOdokCXA5qO" }, { "cell_type": "code", "execution_count": 16, "metadata": { "id": "2jKWEdVMA5qO", "outputId": "26968974-fd9b-40c1-efd5-4031c9e92459", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[[1, 'Identificación'],\n", " [2, 'Exploración'],\n", " [3, 'Diseño'],\n", " [4, 'Planificación'],\n", " [5, 'Construcción'],\n", " [6, 'Evaluación'],\n", " [7, 'Divulgación']]" ] }, "metadata": {}, "execution_count": 16 } ], "source": [ "# primero ordenamos las fases\n", "l.sort()\n", "l" ], "id": "2jKWEdVMA5qO" }, { "cell_type": "code", "execution_count": 17, "metadata": { "id": "DDG84TA3A5qO", "outputId": "f7a48f83-f6b2-4ad9-c5e0-910622c67471", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "deque(['Identificación',\n", " 'Exploración',\n", " 'Diseño',\n", " 'Planificación',\n", " 'Construcción',\n", " 'Evaluación',\n", " 'Divulgación'])" ] }, "metadata": {}, "execution_count": 17 } ], "source": [ "from collections import deque\n", "cola = deque() # creando una cola vacía\n", "for fase in fases:\n", " cola.append(fase)\n", "cola" ], "id": "DDG84TA3A5qO" }, { "cell_type": "code", "execution_count": 18, "metadata": { "id": "pjzzYkeuA5qO", "outputId": "9eb4e10d-d133-4a0d-bb19-d4aad07e4664", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Identificación\n", "Exploración\n", "Diseño\n", "Planificación\n", "Construcción\n", "Evaluación\n", "Divulgación\n" ] } ], "source": [ "for fase in cola:\n", " print(fase)" ], "id": "pjzzYkeuA5qO" }, { "cell_type": "markdown", "metadata": { "id": "S-koShUtA5qO" }, "source": [ "Ver que fase sería la que toca realizar." ], "id": "S-koShUtA5qO" }, { "cell_type": "code", "execution_count": 19, "metadata": { "id": "1e3eEicyA5qO", "outputId": "5464a011-95a9-4f9e-bd09-804645047380", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Identificación'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 19 } ], "source": [ "cola.popleft()" ], "id": "1e3eEicyA5qO" } ], "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.8.5" }, "colab": { "name": "0440_pilas_y_colas.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }