From 88f1aad400d837e0052786dbb7305cc88f79af47 Mon Sep 17 00:00:00 2001 From: MonicaFernandezM Date: Mon, 12 Jan 2026 20:07:11 +0100 Subject: [PATCH 1/2] Update-data-structures-extra --- lab-python-data-structures.ipynb | 335 +++++++++++++++++++++++++++++-- 1 file changed, 316 insertions(+), 19 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..227789c 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -57,11 +57,74 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "# prompt the teacher to enter the grades of each student. \n", + "student1 = float(input(\"Enter grade for student 1: \"))\n", + "student2 = float(input(\"Enter grade for student 2: \"))\n", + "student3 = float(input(\"Enter grade for student 3: \"))\n", + "student4 = float(input(\"Enter grade for student 4: \"))\n", + "student5 = float(input(\"Enter grade for student 5: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "32.0\n" + ] + } + ], + "source": [ + "#Save all the grades on a list\n", + "grades = [student1, student2, student3, student4, student5]\n", + "\n", + "#Calculate the total grades \n", + "total_grades = sum(grades)\n", + "print(total_grades)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "choose_grades = grades[0:5:2]\n", + "choose_grades.sort()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5.0, 5.0, 9.0]\n", + "3\n", + "Number of occurrences of the score 5: 2\n" + ] + } + ], + "source": [ + "#print the new list \n", + "print(choose_grades)\n", + "\n", + "#length of the list\n", + "print(len(choose_grades))\n", + "\n", + "score5 = choose_grades.count(5)\n", + "print(f\"Number of occurrences of the score 5: {score5}\")" ] }, { @@ -95,11 +158,117 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "fruit = (\"kiwi\", \"apple\", \"watermelon\", \"blueberry\", \"orange\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "kiwi\n", + "orange\n" + ] + } + ], + "source": [ + "#outputs the first and last elements of the tuple\n", + "print(fruit[0])\n", + "print(fruit[-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[19], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m#replaces the second element of the tuple with a new fruit \u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mfruit\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgrape\u001b[39m\u001b[38;5;124m'\u001b[39m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "#replaces the second element of the tuple with a new fruit \n", + "fruit[1] = 'grape'" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original Tuple: ('kiwi', 'apple', 'watermelon', 'blueberry', 'orange')\n", + "Second Tuple ('Pineaple', 'Strawberry')\n", + "Updated inventory ('kiwi', 'apple', 'watermelon', 'blueberry', 'orange', 'Pineaple', 'Strawberry')\n" + ] + } + ], + "source": [ + "#Create a new tuple and concatenates to the original tuple/\n", + "new_tuple = (\"Pineaple\", \"Strawberry\")\n", + "store_inventory = fruit + new_tuple\n", + "print(\"Original Tuple: \", fruit)\n", + "print(\"Second Tuple \", new_tuple)\n", + "print(\"Updated inventory \", store_inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('kiwi', 'apple', 'watermelon') ('blueberry', 'orange', 'Pineaple')\n" + ] + } + ], + "source": [ + "#Splits the resulting tuple into 2 tuples of 3 elements each\n", + "first_tuple = store_inventory[:3]\n", + "second_tuple = store_inventory[3:6]\n", + "print(first_tuple, second_tuple)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Final inventory after all the changes: ('kiwi', 'apple', 'watermelon', 'blueberry', 'orange', 'Pineaple', 'kiwi', 'apple', 'watermelon', 'blueberry', 'orange')\n", + "Length of final inventory: 11\n" + ] + } + ], + "source": [ + "final_inventory = first_tuple + second_tuple + fruit\n", + "print(\"Final inventory after all the changes: \", final_inventory)\n", + "print(\"Length of final inventory: \", len(final_inventory))" ] }, { @@ -136,7 +305,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +332,101 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "import re\n", + "\n", + "# 1. Clean de poem \n", + "clean_poem_1 = re.sub(r\"[^a-zA-Z0-9\\s]\", \"\", poem).lower()\n", + "clean_poem_2 = re.sub(r\"[^a-zA-Z0-9\\s]\", \"\", new_poem).lower()" + ] + }, + { + "cell_type": "code", + "execution_count": 37, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "set_1 = set(clean_poem_1.split())\n", + "set_2 = set(clean_poem_2.split())" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of unique words in poem: 41\n", + "Number of unique words in new_poem: 42\n" + ] + } + ], + "source": [ + "print(f\"Number of unique words in poem: {len(set_1)}\")\n", + "print(f\"Number of unique words in new_poem: {len(set_2)}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "words in poem but not in new_poem: {'would', 'the', 'perish', 'fire', 'twice', 'for', 'suffice', 'will', 'destruction', 'hate', 'ice', 'desire', 'favor', 'world', 'also', 'hold', 'great', 'in', 'tasted'}\n" + ] + } + ], + "source": [ + "# 4. dentify and print the unique words present in the first poem but not in the second one.\n", + "unique_poem = set_1 - set_2\n", + "print(f\"words in poem but not in new_poem: {unique_poem}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "words in new poem but not in poem: {'test', 'its', 'as', 'made', 'side', 'love', 'we', 'dream', 'deem', 'today', 'still', 'a', 'seen', 'quest', 'life', 'are', 'fades', 'away', 'though', 'see'}\n" + ] + } + ], + "source": [ + "# 5. dentify and print the unique words present in the second poem but not in the first one.\n", + "unique_new_poem = set_2 - set_1\n", + "print(f\"words in new poem but not in poem: {unique_new_poem}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Words present in both poems in alphabetical order: ['and', 'but', 'end', 'enough', 'from', 'had', 'i', 'if', 'is', 'it', 'ive', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 'what', 'who', 'with']\n" + ] + } + ], + "source": [ + "# 6. Identify and print the unique words present in both poems and print it in alphabetical order.\n", + "common_words = sorted(set_1 & set_2)\n", + "print(f\"Words present in both poems in alphabetical order: {common_words}\")\n" ] }, { @@ -193,7 +452,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +461,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 100, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}}\n" + ] + } + ], "source": [ - "# Your code here" + "# Score in Philosophy to 100\n", + "grades['Bob']['Physics'] = 100\n", + "\n", + "print(grades)" ] }, { @@ -239,14 +509,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "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" + "# Convert them into a dictionary \n", + "dict_merge = dict(zip(keys, values))\n", + "#print the dictionary\n", + "print(dict_merge)" ] }, { @@ -275,17 +556,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Subject with the minimum score: Chemistry\n" + ] + } + ], "source": [ - "# Your code here" + "minimum_value = min(dict_merge, key=dict_merge.get)\n", + "print(f\"Subject with the minimum score: {minimum_value}\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -299,7 +596,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.2" } }, "nbformat": 4, From b713708cabf93d7473a40d6a7fc28ff24006064c Mon Sep 17 00:00:00 2001 From: MonicaFernandezM Date: Mon, 12 Jan 2026 20:11:00 +0100 Subject: [PATCH 2/2] update-feedback --- lab-python-data-structures.ipynb | 46 +++++++++++++------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 227789c..65804d3 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -158,7 +158,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ @@ -167,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 49, "metadata": {}, "outputs": [ { @@ -187,38 +187,28 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 50, "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'tuple' object does not support item assignment", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[19], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m#replaces the second element of the tuple with a new fruit \u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mfruit\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgrape\u001b[39m\u001b[38;5;124m'\u001b[39m\n", - "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" - ] - } - ], + "outputs": [], "source": [ "#replaces the second element of the tuple with a new fruit \n", - "fruit[1] = 'grape'" + "lista_frutas = list(fruit)\n", + "lista_frutas[1] = 'grape'\n", + "fruit = tuple(lista_frutas)" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Original Tuple: ('kiwi', 'apple', 'watermelon', 'blueberry', 'orange')\n", + "Original Tuple: ('kiwi', 'grape', 'watermelon', 'blueberry', 'orange')\n", "Second Tuple ('Pineaple', 'Strawberry')\n", - "Updated inventory ('kiwi', 'apple', 'watermelon', 'blueberry', 'orange', 'Pineaple', 'Strawberry')\n" + "Updated inventory ('kiwi', 'grape', 'watermelon', 'blueberry', 'orange', 'Pineaple', 'Strawberry')\n" ] } ], @@ -233,14 +223,14 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "('kiwi', 'apple', 'watermelon') ('blueberry', 'orange', 'Pineaple')\n" + "('kiwi', 'grape', 'watermelon') ('blueberry', 'orange', 'Pineaple')\n" ] } ], @@ -253,14 +243,14 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Final inventory after all the changes: ('kiwi', 'apple', 'watermelon', 'blueberry', 'orange', 'Pineaple', 'kiwi', 'apple', 'watermelon', 'blueberry', 'orange')\n", + "Final inventory after all the changes: ('kiwi', 'grape', 'watermelon', 'blueberry', 'orange', 'Pineaple', 'kiwi', 'grape', 'watermelon', 'blueberry', 'orange')\n", "Length of final inventory: 11\n" ] } @@ -452,7 +442,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ @@ -461,20 +451,20 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 100, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}}\n" + "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}\n" ] } ], "source": [ "# Score in Philosophy to 100\n", - "grades['Bob']['Physics'] = 100\n", + "grades['Bob']['Philosophy'] = 100\n", "\n", "print(grades)" ]