From 942fb96990d8587c693171c8e4deed39aeda5a5e Mon Sep 17 00:00:00 2001 From: Juan Carlos Del Mar Lostanau <147656412+Juancarlosdelmarlostanau@users.noreply.github.com> Date: Sat, 19 Sep 2026 20:27:23 +0200 Subject: [PATCH] Update lab-python-data-structures.ipynb solucion1 --- lab-python-data-structures.ipynb | 333 ++++++++++++++++++++++++++----- 1 file changed, 288 insertions(+), 45 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..ff1f3ca 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -18,31 +18,31 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Imagine you are building a program for a teacher who wants to track the progress of their students throughout the semester. The teacher wants to input the grades of each student one by one, and get a summary of their performance. There are in total 5 students. You are tasked with building the program that will allow the teacher to do this easily.\n", + "Imagine que está creando un programa para un profesor que quiere realizar un seguimiento del progreso de sus alumnos a lo largo del semestre. El profesor quiere ingresar las calificaciones de cada estudiante una por una y obtener un resumen de su desempeño. Hay en total 5 estudiantes. Usted tiene la tarea de crear el programa que permitirá al profesor hacer esto fácilmente.\n", "\n", - "The program will prompt the teacher to enter the grades of each student. Once the teacher has entered all the grades, the program will calculate the total sum of the grades and display it on the screen. Then, the program will create a new list by selecting only the grades of the first, third, and fifth students entered by the teacher, and sort them in ascending order.\n", + "El programa le pedirá al maestro que ingrese las calificaciones de cada estudiante. Una vez que el profesor haya ingresado todas las calificaciones, el programa calculará la suma total de las calificaciones y la mostrará en pantalla. Luego, el programa creará una nueva lista seleccionando solo las calificaciones del primer, tercer y quinto estudiante ingresadas por el maestro y las ordenará en orden ascendente.\n", "\n", - "Finally, the program will print out the new list, along with its length and the number of occurrences of the score 5 in the list. This will give the teacher a good overview of the performance of the selected students, and help them identify any potential issues early on." + "Finalmente, el programa imprimirá la nueva lista, junto con su longitud y el número de apariciones de la puntuación 5 en la lista. Esto le dará al profesor una buena visión general del desempeño de los estudiantes seleccionados y les ayudará a identificar cualquier problema potencial desde el principio." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "*Hint:*\n", - "- You can use the input() function to ask the user to enter their information.\n", - "- Look for list methods to perform the tasks. \n", - "- Remember, it is possible to get a part of the sequence using:\n", + "*Pista:*\n", + "- Puede utilizar la función input() para pedirle al usuario que ingrese su información.\n", + "- Busque métodos de lista para realizar las tareas. \n", + "- Recuerda, es posible obtener una parte de la secuencia usando:\n", "\n", - " ```python\n", - " sequence[x:y:z]\n", + " ```pitón\n", + " secuencia[x:y:z]\n", " ```\n", - " where x, y, z are integers.\n", + " donde x, y, z son números enteros.\n", "\n", - " The above returns a new sequence with the following characteristics:\n", + " Lo anterior devuelve una nueva secuencia con las siguientes características:\n", "\n", - " - A sequence with the same type as the original (a slice of a list is a list, a slice of a tuple is a tuple, and a slice of a string is a string).\n", - " - A sequence with elements from `sequence [x]` to `sequence [y-1]` (does not include a sequence [y]). By skipping `z` elements each time, it can be omitted if ` z = 1`.\n" + " - Una secuencia del mismo tipo que la original (un segmento de una lista es una lista, un segmento de una tupla es una tupla y un segmento de una cadena es una cadena).\n", + " - Una secuencia con elementos desde `secuencia [x]` hasta `secuencia [y-1]` (no incluye una secuencia [y]). Al omitir elementos `z` cada vez, se puede omitir si `z = 1`." ] }, { @@ -59,9 +59,56 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1.0, 2.0, 3.0, 4.0, 5.0)\n" + ] + }, + { + "data": { + "text/plain": [ + "15.0" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nota_1 = float(input(\"Ingresa la calificacion del estudiante 1:\"))\n", + "nota_2 = float(input(\"Ingresa la calificacion del estudiante 2:\"))\n", + "nota_3 = float(input(\"Ingresa la calificacion del estudiante 3:\"))\n", + "nota_4 = float(input(\"Ingresa la calificacion del estudiante 4:\"))\n", + "nota_5 = float(input(\"Ingresa la calificacion del estudiante 5:\"))\n", + "\n", + "lista_notas = [nota_1, nota_2, nota_3, nota_4, nota_5]\n", + "print(lista_notas)\n", + "sum(lista_notas)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1.0, 3.0, 5.0)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "nueva_lista =lista_notas[0:5:2]\n", + "nueva_lista" ] }, { @@ -75,14 +122,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Imagine you're running a fruit stand and want to keep track of your inventory. Write a Python program that does the following:\n", + "Imagine que tiene un puesto de frutas y desea realizar un seguimiento de su inventario. Escriba un programa Python que haga lo siguiente:\n", "\n", - "- Initializes a tuple with 5 different types of fruit.\n", - "- Outputs the first and last elements of the tuple, so you can see the full range of fruits the store offers.\n", - "- Replaces the second element of the tuple with a new fruit that the store has recently received, and prints the updated tuple so you can see the changes.\n", - "- Concatenates a new tuple containing 2 additional fruits to the original tuple, so you can add them to the store inventory, and prints the resulting tuple to see the updated inventory.\n", - "- Splits the resulting tuple into 2 tuples of 3 elements each (the first tuple contains the first 3 elements, and the second tuple contains the last 3 elements), so you can organize the inventory more effectively.\n", - "- Combines the 2 tuples from the previous step with the original tuple into a new tuple, and prints the resulting tuple and its length, so you can see the final inventory after all the changes." + "- Inicializar una tupla con 5 tipos diferentes de frutas.\n", + "- Genera el primer y último elemento de la tupla, para que puedas ver toda la gama de frutas que ofrece la tienda.\n", + "- Reemplazar el segundo elemento de la tupla con una nueva fruta que la tienda recibió recientemente e imprimir la tupla actualizada para que puedas ver los cambios.\n", + "- Concatenar una nueva tupla que contenga 2 frutas adicionales a la tupla original, para poder agregarlas al inventario de la tienda, e imprimir la tupla resultante para ver el inventario actualizado.\n", + "- Divide la tupla resultante en 2 tuplas de 3 elementos cada una (la primera tupla contiene los 3 primeros elementos y la segunda tupla contiene los 3 últimos elementos), para que puedas organizar el inventario de forma más efectiva.\n", + "- Combina las 2 tuplas del paso anterior con la tupla original en una nueva tupla, e imprime la tupla resultante y su longitud, para que puedas ver el inventario final después de todos los cambios." ] }, { @@ -95,11 +142,96 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "manzana fresa\n" + ] + } + ], + "source": [ + "lista_frutas = (\"manzana\", \"pera\", \"naranja\", \"sandia\", \"fresa\")\n", + "print((lista_frutas[0]), (lista_frutas[-1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('manzana', 'plantano', 'naranja', 'sandia', 'fresa')\n" + ] + } + ], + "source": [ + "lista_frutas2 = lista_frutas[:1] + ('plantano',) + lista_frutas[2:]\n", + "print(lista_frutas2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('manzana', 'pera', 'naranja', 'sandia', 'fresa', 'arandanos', 'lucuma')\n" + ] + } + ], + "source": [ + "frutas = (\"arandanos\", \"lucuma\")\n", + "inventario = lista_frutas + frutas\n", + "print(inventario)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('manzana', 'pera', 'naranja')\n", + "('fresa', 'arandanos', 'lucuma')\n" + ] + } + ], "source": [ - "# Your code here" + "inventario_sub1 = inventario[:3]\n", + "print(inventario_sub1)\n", + "inventario_sub2 = inventario[4:]\n", + "print(inventario_sub2)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('manzana', 'pera', 'naranja', 'sandia', 'fresa', 'manzana', 'pera', 'naranja', 'fresa', 'arandanos', 'lucuma') 11\n" + ] + } + ], + "source": [ + "inventario_final = lista_frutas + inventario_sub1 + inventario_sub2\n", + "print((inventario_final), len(inventario_final))" ] }, { @@ -136,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -154,7 +286,6 @@ "Some say it's a test.\n", "From what I've seen and what I deem,\n", "I side with those who see it as a quest.\n", - "\n", "But if it had to end today,\n", "I think I know enough of love,\n", "To say that though it fades away,\n", @@ -163,11 +294,96 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "import string" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "poem_limpio = poem.lower().translate(str.maketrans(\"\", \"\", string.punctuation + \"’\"))\n", + "new_poem_limpio = new_poem.lower().translate(str.maketrans(\"\", \"\", string.punctuation + \"’\"))\n", + "set_poem = set(poem_limpio.split())\n", + "set_new_poem = set(new_poem_limpio.split())" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "41\n", + "42\n" + ] + } + ], + "source": [ + "print(len(set_poem))\n", + "print(len(set_new_poem))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'in', 'suffice', 'also', 'hate', 'fire', 'hold', 'desire', 'world', 'tasted', 'perish', 'destruction', 'great', 'twice', 'the', 'favor', 'for', 'would', 'will', 'ice'}\n" + ] + } + ], + "source": [ + "set_identify1 = (set_poem) - (set_new_poem)\n", + "print(set_identify1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'its', 'today', 'quest', 'we', 'fades', 'though', 'life', 'side', 'seen', 'see', 'a', 'love', 'dream', 'deem', 'made', 'still', 'are', 'test', 'as', 'away'}\n" + ] + } + ], + "source": [ + "set_identify2 = (set_new_poem) - (set_poem)\n", + "print(set_identify2)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['and', 'but', 'end', 'enough', 'from', 'had', 'i', 'if', 'is', 'it', 'ive', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 'what', 'who', 'with']\n" + ] + } + ], + "source": [ + "set_identify3 = set_poem & set_new_poem\n", + "print(sorted(set_identify3))" ] }, { @@ -181,7 +397,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Consider the following dictionary of students with their scores in different subjects. One of the students, Bob, has complained about his score in Philosophy and, after reviewing it, the teacher has decided to update his score to 100. Write a Python program that updates Bob's score in Philosophy to 100 in the dictionary." + "Considere el siguiente diccionario de estudiantes con sus puntuaciones en diferentes materias. Uno de los estudiantes, Bob, se ha quejado de su puntuación en Filosofía y, tras revisarla, el profesor ha decidido actualizar su puntuación a 100. Escribe un programa en Python que actualice la puntuación de Bob en Filosofía a 100 en el diccionario." ] }, { @@ -193,7 +409,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +418,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}\n" + ] + } + ], "source": [ - "# Your code here" + "grades['Bob']['Philosophy'] = 100\n", + "print(grades)" ] }, { @@ -220,14 +445,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "1. Below are the two lists. Write a Python program to convert them into a dictionary in a way that item from list1 is the key and item from list2 is the value." + "1. A continuación se muestran las dos listas. Escriba un programa Python para convertirlos en un diccionario de manera que el elemento de la lista1 sea la clave y el elemento de la lista2 sea el valor." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "*Hint: Use the zip() function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it. Afterwards, you can use a function that turns a tuple into a dictionary.*" + "*Sugerencia: utilice la función zip(). Esta función toma dos o más iterables (como lista, dictado, cadena), los agrega en una tupla y la devuelve. Luego, puedes usar una función que convierte una tupla en un diccionario.*" ] }, { @@ -239,21 +464,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n" + ] + } + ], "source": [ "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", "\n", - "# Your code here" + "notas = {key: value for key, value in zip(keys, values)}\n", + "print(notas)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "2. Get the subject with the minimum score from the previous dictionary." + "2. Obtener la asignatura con la puntuación mínima del diccionario anterior." ] }, { @@ -275,17 +509,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Chemistry\n" + ] + } + ], "source": [ - "# Your code here" + "asignatura_min = min(notas, key = notas.get)\n", + "print(asignatura_min)\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -299,7 +542,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,