From 2962b14c62bf23d0a5b649d77d96e940015f7fb7 Mon Sep 17 00:00:00 2001 From: ruiparreira75 Date: Mon, 22 Dec 2025 15:08:09 +0000 Subject: [PATCH 1/2] lab solved --- lab-python-data-structures.ipynb | 427 +++++++++++++++++++++++++++++-- 1 file changed, 410 insertions(+), 17 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..ba5a564 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -57,11 +57,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the grade for student 1: 5\n", + "Enter the grade for student 2: 3\n", + "Enter the grade for student 3: 2\n", + "Enter the grade for student 4: 5\n", + "Enter the grade for student 5: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2.0, 5.0, 5.0]\n", + "3\n", + "2\n" + ] + } + ], "source": [ - "# Your code here" + "grades = [] \n", + "for i in range(5): \n", + " grade = float(input(f\"Enter the grade for student {i+1}: \"))\n", + " grades.append(grade) \n", + "\n", + "sum_grades = sum(grades)\n", + "\n", + "grades_list = grades[::2] \n", + "grades_list = sorted(grades_list, reverse=False) \n", + "\n", + "\n", + "print(grades_list)\n", + "print(len(grades_list)) \n", + "print(grades_list.count(5)) " ] }, { @@ -93,15 +127,164 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('apple', 'orange', 'banana', 'peach', 'grapple')" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Initializes a tuple with 5 different types of fruit. \n", + "fruits = (\"apple\", \"orange\", \"banana\", \"peach\", \"grapple\") \n", + "fruits" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple grapple\n" + ] + } + ], + "source": [ + "# Outputs the first and last elements of the tuple, so you can see the full range of fruits the store offers.\n", + "print(fruits[0], fruits[-1]) " + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('apple', 'tangerine', 'banana', 'peach', 'grapple')\n" + ] + } + ], + "source": [ + "# 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", + "fruits_modify = list(fruits) \n", + "fruits_modify[1] = \"tangerine\" \n", + "fruits = tuple(fruits_modify)\n", + "print(fruits) " + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# 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. " + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('apple', 'tangerine', 'banana', 'peach', 'grapple', 'berrie', 'lemon')" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fruits_2 = (\"berrie\", \"lemon\") \n", + "fruits_new = fruits + fruits_2 \n", + "fruits_new" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Splits the resulting tuple into 2 tuples of 3 elements each (the first tuple contains the first 3 elements, \n", + "# and the second tuple contains the last 3 elements), so you can organize the inventory more effectively." + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('apple', 'tangerine', 'banana')\n", + "('grapple', 'berrie', 'lemon')\n" + ] + } + ], + "source": [ + "f1 = fruits_new[:3] \n", + "f2 = fruits_new[4:]\n", + "print(f1)\n", + "print(f2) \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Combines the 2 tuples from the previous step with the original tuple into a new tuple, and prints the resulting tuple and its length, \n", + "# so you can see the final inventory after all the changes." + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('apple', 'tangerine', 'banana', 'grapple', 'berrie', 'lemon', 'apple', 'tangerine', 'banana', 'peach', 'grapple')\n" + ] + } + ], + "source": [ + "inventory = f1 + f2 + fruits\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -136,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -167,7 +350,180 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Create two sets, one for each poem, containing all unique words in both poems (ignoring case and punctuation)." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "some say the world will end in fire\n", + "some say in ice\n", + "from what i’ve tasted of desire\n", + "i hold with those who favor fire\n", + "but if it had to perish twice\n", + "i think i know enough of hate\n", + "to say that for destruction ice\n", + "is also great\n", + "and would suffice\n" + ] + } + ], + "source": [ + "import string\n", + "def remove_punctuation(poem):\n", + " return poem.translate(str.maketrans(\"\", \"\", string.punctuation))\n", + "\n", + "def to_lowercase(poem):\n", + " return poem.lower()\n", + "\n", + "clean_poem = remove_punctuation(to_lowercase(poem)) \n", + "print(clean_poem) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "some say life is but a dream\n", + "some say its a test\n", + "from what ive 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", + "its still what we are made of\n" + ] + } + ], + "source": [ + "def remove_punctuation(new_poem):\n", + " return new_poem.translate(str.maketrans(\"\", \"\", string.punctuation))\n", + "\n", + "def to_lowercase(new_poem):\n", + " return new_poem.lower()\n", + "\n", + "clean_new_poem = remove_punctuation(to_lowercase(new_poem)) \n", + "\n", + "print(clean_new_poem)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "44\n", + "46\n" + ] + } + ], + "source": [ + "# Print the number of unique words in each set. \n", + "set_1 = poem.split() \n", + "set_2 = new_poem.split() \n", + "unique_set_1 = set(set_1) \n", + "unique_set_2 = set(set_2) \n", + "print(len(unique_set_1)) \n", + "print(len(unique_set_2)) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Identify and print the unique words present in the first poem but not in the second one." + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'ice.', 'also', 'hold', 'will', 'twice,', 'the', 'world', 'suffice.', 'ice', 'And', 'would', 'for', 'tasted', 'fire.', 'desire', 'destruction', 'hate', 'favor', 'I’ve', 'in', 'fire,', 'great', 'Is', 'perish'}\n" + ] + } + ], + "source": [ + "unique_first = unique_set_1 - unique_set_2\n", + "print(unique_first) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Identify and print the unique words present in the second poem but not in the first one." + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'quest.', 'life', \"it's\", 'see', 'away,', 'is', 'today,', 'but', 'as', 'dream,', 'still', 'are', 'made', 'side', \"It's\", 'of.', 'love,', \"I've\", 'fades', 'we', 'a', 'seen', 'and', 'test.', 'deem,', 'though'}\n" + ] + } + ], + "source": [ + "unique_second = unique_set_2 - unique_set_1\n", + "print(unique_second) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Identify and print the unique words present in both poems and print it in alphabetical order." + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'also', 'And', 'and', 'are', 'as', 'away,', 'But', 'but', 'deem,', 'desire', 'destruction', 'dream,', 'end', 'enough', 'fades', 'favor', 'fire.', 'fire,', 'for', 'From', 'great', 'had', 'hate', 'hold', 'I', 'ice.', 'ice', 'if', 'in', 'Is', 'is', 'it', \"it's\", \"It's\", \"I've\", 'I’ve', 'know', 'life', 'love,', 'made', 'of', 'of.', 'perish', 'quest.', 'say', 'see', 'seen', 'side', 'Some', 'still', 'suffice.', 'tasted', 'test.', 'that', 'the', 'think', 'those', 'though', 'to', 'To', 'today,', 'twice,', 'we', 'what', 'who', 'will', 'with', 'world', 'would']\n" + ] + } + ], + "source": [ + "unique_both = unique_set_1 | unique_set_2 \n", + "def clean(unique_both): return unique_both .lower().translate(str.maketrans(\"\", \"\", string.punctuation)) \n", + "sorted_words = sorted(unique_both , key=clean)\n", + "print(sorted_words) " ] }, { @@ -193,7 +549,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 92, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +558,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "100\n", + "{'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['Bob']['Philosophy']) \n", + "print(grades) " ] }, { @@ -239,14 +606,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 103, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n" + ] + } + ], "source": [ + "# 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.\n", "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", "\n", - "# Your code here" + "grades = zip(keys, values) \n", + "dictionary = dict(grades) \n", + "print (dictionary) " ] }, { @@ -275,12 +653,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 104, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Chemistry\n" + ] + } + ], "source": [ - "# Your code here" + "print (min(dictionary)) " ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -299,7 +692,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From 5320f2fad27fa2fea5f1ac9cea17e153942f5fda Mon Sep 17 00:00:00 2001 From: ruiparreira75 Date: Mon, 22 Dec 2025 15:19:33 +0000 Subject: [PATCH 2/2] lab solved_v2 --- lab-python-data-structures.ipynb | 61 ++++++++++++++++---------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index ba5a564..35abf6d 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -129,7 +129,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -138,7 +138,7 @@ "('apple', 'orange', 'banana', 'peach', 'grapple')" ] }, - "execution_count": 72, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -151,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -169,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -190,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -199,7 +199,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -208,7 +208,7 @@ "('apple', 'tangerine', 'banana', 'peach', 'grapple', 'berrie', 'lemon')" ] }, - "execution_count": 82, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -221,7 +221,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -231,7 +231,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -245,7 +245,7 @@ ], "source": [ "f1 = fruits_new[:3] \n", - "f2 = fruits_new[4:]\n", + "f2 = fruits_new[-3:]\n", "print(f1)\n", "print(f2) \n" ] @@ -319,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -346,7 +346,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -355,7 +355,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -388,7 +388,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -421,22 +421,22 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "44\n", - "46\n" + "41\n", + "42\n" ] } ], "source": [ "# Print the number of unique words in each set. \n", - "set_1 = poem.split() \n", - "set_2 = new_poem.split() \n", + "set_1 = clean_poem.split() \n", + "set_2 = clean_new_poem.split() \n", "unique_set_1 = set(set_1) \n", "unique_set_2 = set(set_2) \n", "print(len(unique_set_1)) \n", @@ -454,14 +454,14 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'ice.', 'also', 'hold', 'will', 'twice,', 'the', 'world', 'suffice.', 'ice', 'And', 'would', 'for', 'tasted', 'fire.', 'desire', 'destruction', 'hate', 'favor', 'I’ve', 'in', 'fire,', 'great', 'Is', 'perish'}\n" + "{'great', 'favor', 'for', 'world', 'destruction', 'twice', 'perish', 'ice', 'would', 'tasted', 'desire', 'the', 'hate', 'also', 'hold', 'in', 'will', 'suffice', 'i’ve', 'fire'}\n" ] } ], @@ -481,14 +481,14 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'quest.', 'life', \"it's\", 'see', 'away,', 'is', 'today,', 'but', 'as', 'dream,', 'still', 'are', 'made', 'side', \"It's\", 'of.', 'love,', \"I've\", 'fades', 'we', 'a', 'seen', 'and', 'test.', 'deem,', 'though'}\n" + "{'we', 'side', 'are', 'made', 'see', 'test', 'life', 'fades', 'still', 'love', 'ive', 'its', 'away', 'today', 'quest', 'a', 'seen', 'though', 'dream', 'as', 'deem'}\n" ] } ], @@ -508,14 +508,14 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "['a', 'also', 'And', 'and', 'are', 'as', 'away,', 'But', 'but', 'deem,', 'desire', 'destruction', 'dream,', 'end', 'enough', 'fades', 'favor', 'fire.', 'fire,', 'for', 'From', 'great', 'had', 'hate', 'hold', 'I', 'ice.', 'ice', 'if', 'in', 'Is', 'is', 'it', \"it's\", \"It's\", \"I've\", 'I’ve', 'know', 'life', 'love,', 'made', 'of', 'of.', 'perish', 'quest.', 'say', 'see', 'seen', 'side', 'Some', 'still', 'suffice.', 'tasted', 'test.', 'that', 'the', 'think', 'those', 'though', 'to', 'To', 'today,', 'twice,', 'we', 'what', 'who', 'will', 'with', 'world', 'would']\n" + "['a', 'also', 'and', 'are', 'as', 'away', 'but', 'deem', 'desire', 'destruction', 'dream', 'end', 'enough', 'fades', 'favor', 'fire', 'for', 'from', 'great', 'had', 'hate', 'hold', 'i', 'ice', 'if', 'in', 'is', 'it', 'its', 'ive', 'i’ve', 'know', 'life', 'love', 'made', 'of', 'perish', 'quest', 'say', 'see', 'seen', 'side', 'some', 'still', 'suffice', 'tasted', 'test', 'that', 'the', 'think', 'those', 'though', 'to', 'today', 'twice', 'we', 'what', 'who', 'will', 'with', 'world', 'would']\n" ] } ], @@ -549,7 +549,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -558,7 +558,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -606,7 +606,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -653,7 +653,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -665,7 +665,8 @@ } ], "source": [ - "print (min(dictionary)) " + "min_subject = min(dictionary, key=dictionary.get)\n", + "print(min_subject)" ] }, {