From b804638bff2f8b0273d3dd0598a4ce4fa26f10bd Mon Sep 17 00:00:00 2001 From: Estherkii <123992666+Estherkii@users.noreply.github.com> Date: Tue, 10 Oct 2023 20:55:09 +0200 Subject: [PATCH 1/2] lab-dict-sets-tuple --- your-code/challenge-1.ipynb | 214 +++++++++++++++++++---- your-code/challenge-2.ipynb | 331 +++++++++++++++++++++++++++++------- your-code/challenge-3.ipynb | 65 ++++++- 3 files changed, 509 insertions(+), 101 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 3ff7f9b..e196578 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -14,11 +14,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "tup = (\"I\",) " ] }, { @@ -32,11 +32,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "type(tup)" ] }, { @@ -54,13 +65,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n", + "tup_origin = (\"I\",) \n", + "\n", + "tup_adding = (\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\",) #we create a new tup for adding it later by +\n", + "\n", + "tup_append = tup_origin + tup_adding\n", "\n", - "# Your explanation here\n" + "print(tup_append)\n", + " \n", + "# You can't add elements to a tuple because of their immutable property. There's no append() or extend() method for tuples so we add another one tuple and sum them together" ] }, { @@ -80,13 +105,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n", + "tup = list(tup_origin)\n", + "\n", + "tup_new_values = \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"\n", + "\n", + "tup.extend(tup_new_values)\n", "\n", - "# Your explanation here\n" + "print(tuple(tup))\n", + "\n", + "\n", + "# We cannot add elements to a tuple so we convert it into a list, add elements and convert again in tuple\n" ] }, { @@ -104,11 +144,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['I', 'r', 'o', 'n']\n", + "['h', 'a', 'c', 'k']\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup1 = tup[:4]\n", + "\n", + "tup2 = tup[4:]\n", + "\n", + "print(tup1)\n", + "print(tup2)" ] }, { @@ -122,11 +176,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['I', 'r', 'o', 'n', 'h', 'a', 'c', 'k']\n", + "Result is: False\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup3 = tup1 + tup2\n", + "print(tup3)\n", + "\n", + "# initialize list of tuples \n", + "tup1 = ['I', 'r', 'o', 'n']\n", + "tup2 = ['h', 'a', 'c', 'k']\n", + " \n", + "# convert lists to sets\n", + "set1 = set(tup1)\n", + "set2 = set(tup2)\n", + " \n", + "# check if sets are identical\n", + "res = set1 == set2\n", + "print(\"Result is: \", (res))" ] }, { @@ -138,11 +214,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "4\n", + "Result is: True\n" + ] + } + ], "source": [ - "# Your code here\n" + "#count the number of elements in tup1 and tup2\n", + "\n", + "tup4 = len(tup1)\n", + "print(tup4)\n", + "tup5 = len(tup1)\n", + "print(tup5)\n", + "\n", + "# check if sets are identical\n", + "res = tup4 == tup5\n", + "print(\"Result is: \", (res))" ] }, { @@ -154,11 +249,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The index number of h is: 4\n" + ] + } + ], "source": [ - "# Your code here\n" + "# tuple containing vowels\n", + "tup3 = ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k',)\n", + "\n", + "# index of 'e' in vowels\n", + "index = tup3.index('h')\n", + "\n", + "print(\"The index number of h is: \", index)" ] }, { @@ -178,11 +287,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "False\n", + "False\n" + ] + } + ], "source": [ - "# Your code here\n" + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "for element in letters:\n", + " if element in tup3:\n", + " print(\"True\")\n", + " else: \n", + " print(\"False\")" ] }, { @@ -203,11 +330,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a: 1\n", + "b: 0\n", + "c: 1\n", + "d: 0\n", + "e: 0\n" + ] + } + ], "source": [ - "# Your code here\n" + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "tup3 = ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "\n", + "# Create a dictionary\n", + "letter_counts = {}\n", + "\n", + "for letter in letters:\n", + " count = tup3.count(letter)\n", + " letter_counts[letter] = count\n", + "\n", + "for letter, count in letter_counts.items():\n", + " print(f\"{letter}: {count}\")\n" ] } ], @@ -227,7 +377,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.10.9" }, "toc": { "base_numbering": 1, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 49072f2..3ef8f5d 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -47,11 +47,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[15.916644426710391, 78.94881937576783, 40.24477755723228, 34.14921518490173, 97.18007088115624, 85.50752756824687, 16.68730704672545, 30.618026818214616, 12.126654837790873, 37.2679714475359, 32.03629919498058, 53.187861654687715, 92.09370678987545, 12.481777397392024, 47.04517794525027, 81.80945487818069, 96.27038803031687, 72.53111309030287, 79.61880449686633, 73.28361121480732, 95.7909198995813, 82.53715729561554, 39.38819320499882, 23.342008223947673, 0.8383571856863892, 44.37676065758303, 51.17888640339291, 46.74727106342924, 24.53640775487438, 89.33398730501085, 81.93688380190332, 28.23592604298304, 34.18736279300535, 73.4439474392637, 53.074131156745366, 25.03375630262684, 41.23862412361197, 89.68160475447522, 77.23938435227859, 8.808652851836541, 63.38258498534428, 26.586171790889722, 50.7603135853296, 73.47231256181708, 87.29671767823893, 75.74163881528293, 18.862465302377117, 58.02653926183093, 16.244707935313485, 77.98253520171474, 44.22195361329242, 46.358608299737845, 63.990061460083304, 32.63299289743994, 23.34862027079415, 21.499452455863256, 86.15252155802084, 62.56598245210338, 58.95386924514021, 22.991164290777267, 34.7346410726613, 51.71508293499969, 66.55885224253376, 7.582864682545932, 0.47253432057959843, 93.69885799443958, 20.835210788722247, 54.984203571312406, 68.46171052251607, 36.814622495619496, 71.36419500412565, 6.716642885024305, 76.73635231415123, 47.06595040761169, 59.44605325786934, 92.3353267216173, 15.994156058594955, 73.90455783123426, 20.37348171231169, 70.9981307104103, 46.62028529972052]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# In the cell below, create a list named sample_list_1 with 80 random values.\n", + "# Requirements:\n", + "# Each value is an integer falling between 0 and 100.\n", + "min_value = 0\n", + "max_value = 100\n", + "\n", + "# Create a list with 80 random values, each value must be unique.\n", + "sample_list_1 = [random.uniform(min_value, max_value) for _ in range(80)]\n", + "\n", + "#each value must be unique\n", + "if new_value not in sample_list_1:\n", + " sample_list_1.append(new_value)\n", + "\n", + "# Print the list\n", + "print(sample_list_1)\n" ] }, { @@ -63,11 +85,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of set1: 81\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = set(sample_list_1)\n", + "\n", + "print(\"Length of set1:\", len(set1))\n", + "\n", + "# No, it is 81" ] }, { @@ -86,11 +120,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[68, 24, 55, 67, 62, 53, 38, 72, 18, 12, 33, 17, 38, 12, 77, 7, 56, 14, 17, 80, 57, 19, 44, 53, 21, 72, 69, 23, 32, 71, 27, 18, 43, 57, 69, 63, 9, 27, 15, 28, 83, 86, 92, 22, 78, 41, 65, 33, 4, 13, 80, 81, 32, 18, 31, 96, 95, 94, 64, 34, 21, 10, 45, 45, 33, 32, 37, 96, 2, 55, 56, 36, 68, 49, 20, 29, 14, 8, 42, 89]\n" + ] + } + ], "source": [ - "# Your code here\n" + "import random\n", + "\n", + "# Create another list named sample_list_2 with 80 random values.\n", + "# Requirements:\n", + "\n", + "#Each value is an integer falling between 0 and 100.\n", + "min_value = 0\n", + "max_value = 100\n", + "\n", + "sample_list_2 = []\n", + "\n", + "for _ in range(80):\n", + " random_integer = random.randint(min_value, max_value)\n", + " sample_list_2.append(random_integer)\n", + "\n", + "print(sample_list_2)" ] }, { @@ -102,11 +159,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of set2: 58\n" + ] + } + ], "source": [ - "# Your code here\n" + "set2 = set(sample_list_2)\n", + "\n", + "print(\"Length of set2:\", len(set2))\n", + "\n", + "# No, it is 58" ] }, { @@ -118,11 +187,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Elements in set1 named set3: {0.8383571856863892, 0.47253432057959843, 6.716642885024305, 7.582864682545932, 8.808652851836541, 12.126654837790873, 12.481777397392024, 15.916644426710391, 16.68730704672545, 16.244707935313485, 18.862465302377117, 15.994156058594955, 20.835210788722247, 21.499452455863256, 22.991164290777267, 23.342008223947673, 24.53640775487438, 25.03375630262684, 26.586171790889722, 23.34862027079415, 28.23592604298304, 20.37348171231169, 30.618026818214616, 32.03629919498058, 32.63299289743994, 34.14921518490173, 34.18736279300535, 34.7346410726613, 37.2679714475359, 36.814622495619496, 39.38819320499882, 40.24477755723228, 41.23862412361197, 44.37676065758303, 44.22195361329242, 46.74727106342924, 47.04517794525027, 46.358608299737845, 47.06595040761169, 50.7603135853296, 51.17888640339291, 51.71508293499969, 53.187861654687715, 53.074131156745366, 54.984203571312406, 58.02653926183093, 58.95386924514021, 59.44605325786934, 62.56598245210338, 63.38258498534428, 63.990061460083304, 66.55885224253376, 68.46171052251607, 70.9981307104103, 71.36419500412565, 72.53111309030287, 73.28361121480732, 73.4439474392637, 73.47231256181708, 75.74163881528293, 77.23938435227859, 78.94881937576783, 79.61880449686633, 77.98253520171474, 81.80945487818069, 82.53715729561554, 81.93688380190332, 76.73635231415123, 85.50752756824687, 86.15252155802084, 87.29671767823893, 89.33398730501085, 89.68160475447522, 92.09370678987545, 93.69885799443958, 92.3353267216173, 95.7909198995813, 96.27038803031687, 97.18007088115624, 46.62028529972052, 73.90455783123426}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set3 = set1 - set2\n", + "\n", + "print(\"Elements in set1 named set3:\", set3)" ] }, { @@ -134,11 +213,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Elements in set2 named set4: {2, 4, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 29, 31, 32, 33, 34, 36, 37, 38, 41, 42, 43, 44, 45, 49, 53, 55, 56, 57, 62, 63, 64, 65, 67, 68, 69, 71, 72, 77, 78, 80, 81, 83, 86, 89, 92, 94, 95, 96}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set4 = set2 - set1\n", + "\n", + "print(\"Elements in set2 named set4:\", set4)" ] }, { @@ -150,11 +239,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{7, 8, 12, 15, 18, 20, 21, 22, 23, 24, 28, 32, 34, 36, 37, 41, 44, 53, 62, 63, 68, 71, 72, 77, 78, 81, 86, 89, 92, 95, 96}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = {0.8383571856863892, 0.47253432057959843, 6.716642885024305, 7.582864682545932, 8.808652851836541, 12.126654837790873, 12.481777397392024, 15.916644426710391, 16.68730704672545, 16.244707935313485, 18.862465302377117, 15.994156058594955, 20.835210788722247, 21.499452455863256, 22.991164290777267, 23.342008223947673, 24.53640775487438, 25.03375630262684, 26.586171790889722, 23.34862027079415, 28.23592604298304, 20.37348171231169, 30.618026818214616, 32.03629919498058, 32.63299289743994, 34.14921518490173, 34.18736279300535, 34.7346410726613, 37.2679714475359, 36.814622495619496, 39.38819320499882, 40.24477755723228, 41.23862412361197, 44.37676065758303, 44.22195361329242, 46.74727106342924, 47.04517794525027, 46.358608299737845, 47.06595040761169, 50.7603135853296, 51.17888640339291, 51.71508293499969, 53.187861654687715, 53.074131156745366, 54.984203571312406, 58.02653926183093, 58.95386924514021, 59.44605325786934, 62.56598245210338, 63.38258498534428, 63.990061460083304, 66.55885224253376, 68.46171052251607, 70.9981307104103, 71.36419500412565, 72.53111309030287, 73.28361121480732, 73.4439474392637, 73.47231256181708, 75.74163881528293, 77.23938435227859, 78.94881937576783, 79.61880449686633, 77.98253520171474, 81.80945487818069, 82.53715729561554, 81.93688380190332, 76.73635231415123, 85.50752756824687, 86.15252155802084, 87.29671767823893, 89.33398730501085, 89.68160475447522, 92.09370678987545, 93.69885799443958, 92.3353267216173, 95.7909198995813, 96.27038803031687, 97.18007088115624, 46.62028529972052, 73.90455783123426}\n", + "set2 = {2, 4, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 29, 31, 32, 33, 34, 36, 37, 38, 41, 42, 43, 44, 45, 49, 53, 55, 56, 57, 62, 63, 64, 65, 67, 68, 69, 71, 72, 77, 78, 80, 81, 83, 86, 89, 92, 94, 95, 96}\n", + "\n", + "set5 = {int(element) for element in set1} & set2\n", + "\n", + "print(set5)" ] }, { @@ -186,11 +288,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of set1: 81\n", + "Length of set2: 58\n", + "Length of set3: 23\n", + "Length of set4: 58\n", + "Length of set5: -58\n" + ] + } + ], "source": [ - "# Your code here\n" + "# What is the relationship among the following values: len(set1) len(set2) len(set3) len(set4) len(set5)\n", + "# I think that the length of set1 is equal to the sum of the lengths set3, set4, and set5, minus the length of set2\n", + "\n", + "len_set1 = len(set1)\n", + "len_set2 = len(set2)\n", + "len_set3 = len(set1) - len(set4 | set5)\n", + "len_set4 = len(set4)\n", + "len_set5 = len(set1) - len(set2) - len(set3)\n", + "\n", + "print(\"Length of set1:\", len_set1)\n", + "print(\"Length of set2:\", len_set2)\n", + "print(\"Length of set3:\", len_set3)\n", + "print(\"Length of set4:\", len_set4)\n", + "print(\"Length of set5:\", len_set5)\n" ] }, { @@ -202,11 +329,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "set6 = set()" ] }, { @@ -218,11 +345,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0.8383571856863892, 0.47253432057959843, 6.716642885024305, 7.582864682545932, 8.808652851836541, 7, 8, 12.126654837790873, 12.481777397392024, 12, 15.916644426710391, 16.68730704672545, 16.244707935313485, 18.862465302377117, 15.994156058594955, 20.835210788722247, 21.499452455863256, 22.991164290777267, 23.342008223947673, 24.53640775487438, 25.03375630262684, 26.586171790889722, 23.34862027079415, 28.23592604298304, 20.37348171231169, 30.618026818214616, 22, 32.03629919498058, 32.63299289743994, 34.14921518490173, 34.18736279300535, 34.7346410726613, 37.2679714475359, 36.814622495619496, 39.38819320499882, 40.24477755723228, 41.23862412361197, 34, 36, 44.37676065758303, 44.22195361329242, 46.74727106342924, 47.04517794525027, 46.358608299737845, 47.06595040761169, 50.7603135853296, 51.17888640339291, 51.71508293499969, 53.187861654687715, 53.074131156745366, 54.984203571312406, 53, 58.02653926183093, 58.95386924514021, 59.44605325786934, 62.56598245210338, 63.38258498534428, 63.990061460083304, 62, 66.55885224253376, 63, 68.46171052251607, 68, 70.9981307104103, 71.36419500412565, 72.53111309030287, 73.28361121480732, 73.4439474392637, 73.47231256181708, 75.74163881528293, 77.23938435227859, 78.94881937576783, 79.61880449686633, 77.98253520171474, 81.80945487818069, 82.53715729561554, 81.93688380190332, 76.73635231415123, 85.50752756824687, 86.15252155802084, 87.29671767823893, 81, 89.33398730501085, 89.68160475447522, 18, 92.09370678987545, 93.69885799443958, 92.3353267216173, 95.7909198995813, 96.27038803031687, 97.18007088115624, 89, 92, 95, 20, 96, 21, 23, 24, 28, 32, 37, 41, 44, 46.62028529972052, 71, 72, 73.90455783123426, 15, 77, 78, 86}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set6.update(set3)\n", + "set6.update(set5)\n", + "\n", + "print(set6)" ] }, { @@ -234,11 +372,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set1 and set6 are not equal\n" + ] + } + ], "source": [ - "# Your code here\n" + "if set1 == set6:\n", + " print(\"set1 and set6 are equal\")\n", + "else:\n", + " print(\"set1 and set6 are not equal\")" ] }, { @@ -250,11 +399,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set1 does not contain set2\n", + "set1 contains set3\n" + ] + } + ], "source": [ - "# Your code here\n" + "#Checking set1/set2\n", + "if set2.issubset(set1):\n", + " print(\"set 1 contains set2\")\n", + "else:\n", + " print(\"set1 does not contain set2\")\n", + "\n", + "#Checking set1/set3\n", + "\n", + "if set3.issubset(set1):\n", + " print(\"set1 contains set3\")\n", + "else:\n", + " print(\"set3 does not contain set1\")" ] }, { @@ -268,11 +437,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sets are equal\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Aggregate set3, set4, and set5\n", + "aggregated_set1 = set3.union(set4, set5)\n", + "\n", + "# Aggregate set1 and set2\n", + "aggregated_set2 = set1.union(set2)\n", + "\n", + "#Are equal?\n", + "if aggregated_set1 == aggregated_set2:\n", + " print(\"Sets are equal\")\n", + "else:\n", + " print(\"Sets are not equal\")" ] }, { @@ -284,11 +471,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Removed first element: 7.582864682545932\n" + ] + } + ], "source": [ - "# Your code here\n" + "if len(set1) > 0:\n", + " removed_element = set1.pop()\n", + " print(\"Removed first element:\", removed_element)" ] }, { @@ -304,11 +501,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Remaining elements: {8.808652851836541, 12.126654837790873, 12.481777397392024, 15.916644426710391, 16.68730704672545, 16.244707935313485, 18.862465302377117, 15.994156058594955, 20.835210788722247, 21.499452455863256, 22.991164290777267, 23.342008223947673, 24.53640775487438, 25.03375630262684, 26.586171790889722, 23.34862027079415, 28.23592604298304, 20.37348171231169, 30.618026818214616, 32.03629919498058, 32.63299289743994, 34.14921518490173, 34.18736279300535, 34.7346410726613, 37.2679714475359, 36.814622495619496, 39.38819320499882, 40.24477755723228, 41.23862412361197, 44.37676065758303, 44.22195361329242, 46.74727106342924, 47.04517794525027, 46.358608299737845, 47.06595040761169, 50.7603135853296, 51.17888640339291, 51.71508293499969, 53.187861654687715, 53.074131156745366, 54.984203571312406, 58.02653926183093, 58.95386924514021, 59.44605325786934, 62.56598245210338, 63.38258498534428, 63.990061460083304, 66.55885224253376, 68.46171052251607, 70.9981307104103, 71.36419500412565, 72.53111309030287, 73.28361121480732, 73.4439474392637, 73.47231256181708, 75.74163881528293, 77.23938435227859, 78.94881937576783, 79.61880449686633, 77.98253520171474, 81.80945487818069, 82.53715729561554, 81.93688380190332, 76.73635231415123, 85.50752756824687, 86.15252155802084, 87.29671767823893, 89.33398730501085, 89.68160475447522, 92.09370678987545, 93.69885799443958, 92.3353267216173, 95.7909198995813, 96.27038803031687, 97.18007088115624, 46.62028529972052, 73.90455783123426}\n" + ] + } + ], "source": [ - "# Your code here\n" + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "\n", + "# Remove every element in the following list from set1 if they are present in the set\n", + "for element in list_to_remove:\n", + " set1.discard(element)\n", + "\n", + "print(\"Remaining elements:\", set1)" ] } ], @@ -328,7 +539,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.10.9" }, "nbTranslate": { "displayLangs": [ diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 0dfb2da..97c9c81 100644 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -45,17 +45,36 @@ "Print out `word_freq2` to examine its keys and values. Your output should be:\n", "\n", "```python\n", - "{'a': 8, 'about': 1, 'all': 1, 'although': 3, 'and': 23, 'are': 1, 'at': 1, 'baby': 14, 'backseat': 1, 'bag': 1, 'bar': 1, 'be': 16, 'bedsheets': 3, 'begin': 1, 'best': 1, 'body': 17, 'boy': 2, 'brand': 6, 'can': 1, 'chance': 1, 'club': 1, 'come': 37, 'conversation': 1, 'crazy': 2, 'dance': 1, 'date': 1, 'day': 6, 'discovering': 6, 'do': 3, 'doing': 2, \"don't\": 2, 'drinking': 1, 'driver': 1, 'eat': 1, 'every': 6, 'falling': 3, 'family': 1, 'fast': 1, 'fill': 2, 'find': 1, 'first': 1, 'follow': 6, 'for': 3, 'friends': 1, 'get': 1, 'girl': 2, 'give': 1, 'go': 2, 'going': 1, 'grab': 2, 'hand': 1, 'handmade': 2, 'heart': 3, 'hours': 2, 'how': 1, 'i': 6, \"i'll\": 1, \"i'm\": 23, 'in': 27, 'is': 5, \"isn't\": 1, 'it': 1, 'jukebox': 1, 'just': 1, 'kiss': 1, 'know': 2, 'last': 3, 'lead': 6, 'leave': 1, 'let': 1, \"let's\": 2, 'like': 10, 'love': 25, 'lover': 1, 'magnet': 3, 'make': 1, 'man': 1, 'may': 2, 'me': 10, 'mind': 2, 'much': 2, 'my': 33, 'new': 6, 'night': 3, 'not': 2, 'now': 11, 'of': 6, 'okay': 1, 'on': 40, 'one': 1, 'our': 1, 'out': 1, 'over': 1, 'place': 1, 'plate': 1, 'play': 1, 'pull': 3, 'push': 3, 'put': 3, 'radio': 1, 'room': 3, 'say': 2, 'shape': 6, 'shots': 1, 'singing': 2, 'slow': 1, 'smell': 3, 'so': 2, 'somebody': 2, 'something': 6, 'sour': 1, 'start': 2, 'stop': 1, 'story': 1, 'sweet': 1, 'table': 1, 'take': 1, 'talk': 4, 'taxi': 1, 'tell': 1, 'that': 2, 'the': 18, 'then': 3, 'thrifty': 1, 'to': 2, 'too': 5, 'trust': 1, 'up': 3, 'van': 1, 'waist': 2, 'want': 2, 'was': 2, 'we': 7, \"we're\": 1, 'week': 1, 'were': 3, 'where': 1, 'with': 22, 'you': 16, 'your': 21}\n", - "```" + "{'a': 8, 'about': 1, 'all': 1, 'although': 3, 'and': 23, 'are': 1, 'at': 1, 'baby': 14, 'backseat': 1, 'bag': 1, 'bar': 1, 'be': 16, 'bedsheets': 3, 'begin': 1, 'best': 1, 'body': 17, 'boy': 2, 'brand': 6, 'can': 1, 'chance': 1, 'club': 1, 'come': 37, 'conversation': 1, 'crazy': 2, 'dance': 1, 'date': 1, 'day': 6, 'discovering': 6, 'do': 3, 'doing': 2, \"don't\": 2, 'drinking': 1, 'driver': 1, 'eat': 1, 'every': 6, 'falling': 3, 'family': 1, 'fast': 1, 'fill': 2, 'find': 1, 'first': 1, 'follow': 6, 'for': 3, 'friends': 1, 'get': 1, 'girl': 2, 'give': 1, 'go': 2, 'going': 1, 'grab': 2, 'hand': 1, 'handmade': 2, 'heart': 3, 'hours': 2, 'how': 1, 'i': 6, \"i'll\": 1, \"i'm\": 23, 'in': 27, 'is': 5, \"isn't\": 1, 'it': 1, 'jukebox': 1, 'just': 1, 'kiss': 1, 'know': 2, 'last': 3, 'lead': 6, 'leave': 1, 'let': 1, \"let's\": 2, 'like': 10, 'love': 25, 'lover': 1, 'magnet': 3, 'make': 1, 'man': 1, 'may': 2, 'me': 10, 'mind': 2, 'much': 2, 'my': 33, 'new': 6, 'night': 3, 'not': 2, 'now': 11, 'of': 6, 'okay': 1, 'on': 40, 'one': 1, 'our': 1, 'out': 1, 'over': 1, 'place': 1, 'plate': 1, 'play': 1, 'pull': 3, 'push': 3, 'put': 3, 'radio': 1, 'room': 3, 'say': 2, 'shape': 6, 'shots': 1, 'singing': 2, 'slow': 1, 'smell': 3, 'so': 2, 'somebody': 2, 'something': 6, 'sour': 1, 'start': 2, 'stop': 1, 'story': 1, 'sweet': 1, 'table': 1, 'take': 1, 'talk': 4, 'taxi': 1, 'tell': 1, 'that': 2, 'the': 18, 'then': 3, 'thrifty': 1, 'to': 2, 'too': 5, 'trust': 1, 'up': 3, 'van': 1, 'waist': 2, 'want': 2, 'was': 2, 'we': 7, \"we're\": 1, 'week': 1, 'were': 3, 'where': 1, 'with': 22, 'you': 16, 'your': 21}\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a': 8, 'about': 1, 'all': 1, 'although': 3, 'and': 23, 'are': 1, 'at': 1, 'baby': 14, 'backseat': 1, 'bag': 1, 'bar': 1, 'be': 16, 'bedsheets': 3, 'begin': 1, 'best': 1, 'body': 17, 'boy': 2, 'brand': 6, 'can': 1, 'chance': 1, 'club': 1, 'come': 37, 'conversation': 1, 'crazy': 2, 'dance': 1, 'date': 1, 'day': 6, 'discovering': 6, 'do': 3, 'doing': 2, \"don't\": 2, 'drinking': 1, 'driver': 1, 'eat': 1, 'every': 6, 'falling': 3, 'family': 1, 'fast': 1, 'fill': 2, 'find': 1, 'first': 1, 'follow': 6, 'for': 3, 'friends': 1, 'get': 1, 'girl': 2, 'give': 1, 'go': 2, 'going': 1, 'grab': 2, 'hand': 1, 'handmade': 2, 'heart': 3, 'hours': 2, 'how': 1, 'i': 6, \"i'll\": 1, \"i'm\": 23, 'in': 27, 'is': 5, \"isn't\": 1, 'it': 1, 'jukebox': 1, 'just': 1, 'kiss': 1, 'know': 2, 'last': 3, 'lead': 6, 'leave': 1, 'let': 1, \"let's\": 2, 'like': 10, 'love': 25, 'lover': 1, 'magnet': 3, 'make': 1, 'man': 1, 'may': 2, 'me': 10, 'mind': 2, 'much': 2, 'my': 33, 'new': 6, 'night': 3, 'not': 2, 'now': 11, 'of': 6, 'okay': 1, 'on': 40, 'one': 1, 'our': 1, 'out': 1, 'over': 1, 'place': 1, 'plate': 1, 'play': 1, 'pull': 3, 'push': 3, 'put': 3, 'radio': 1, 'room': 3, 'say': 2, 'shape': 6, 'shots': 1, 'singing': 2, 'slow': 1, 'smell': 3, 'so': 2, 'somebody': 2, 'something': 6, 'sour': 1, 'start': 2, 'stop': 1, 'story': 1, 'sweet': 1, 'table': 1, 'take': 1, 'talk': 4, 'taxi': 1, 'tell': 1, 'that': 2, 'the': 18, 'then': 3, 'thrifty': 1, 'to': 2, 'too': 5, 'trust': 1, 'up': 3, 'van': 1, 'waist': 2, 'want': 2, 'was': 2, 'we': 7, \"we're\": 1, 'week': 1, 'were': 3, 'where': 1, 'with': 22, 'you': 16, 'your': 21}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# First extract the keys of word_freq and convert it to a list called keys.\n", + "word_freq = {'love': 25, 'conversation': 1, 'every': 6, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'now': 11, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'push': 3, 'baby': 14, 'going': 1, 'you': 16, \"don't\": 2, 'one': 1, 'mind': 2, 'backseat': 1, 'friends': 1, 'then': 3, 'know': 2, 'take': 1, 'play': 1, 'okay': 1, 'so': 2, 'begin': 1, 'start': 2, 'over': 1, 'body': 17, 'boy': 2, 'just': 1, 'we': 7, 'are': 1, 'girl': 2, 'tell': 1, 'singing': 2, 'drinking': 1, 'put': 3, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, \"i'm\": 23, 'like': 10, 'can': 1, 'doing': 2, 'with': 22, 'club': 1, 'come': 37, 'it': 1, 'somebody': 2, 'handmade': 2, 'out': 1, 'new': 6, 'room': 3, 'chance': 1, 'follow': 6, 'in': 27, 'may': 2, 'brand': 6, 'that': 2, 'magnet': 3, 'up': 3, 'first': 1, 'and': 23, 'pull': 3, 'of': 6, 'table': 1, 'much': 2, 'last': 3, 'i': 6, 'thrifty': 1, 'grab': 2, 'was': 2, 'driver': 1, 'slow': 1, 'dance': 1, 'the': 18, 'say': 2, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'me': 10, 'do': 3, 'waist': 2, 'smell': 3, 'day': 6, 'although': 3, 'your': 21, 'leave': 1, 'want': 2, \"let's\": 2, 'lead': 6, 'at': 1, 'hand': 1, 'how': 1, 'talk': 4, 'not': 2, 'eat': 1, 'falling': 3, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'crazy': 2, 'let': 1, 'too': 5, 'van': 1, 'shots': 1, 'go': 2, 'to': 2, 'a': 8, 'my': 33, 'is': 5, 'place': 1, 'find': 1, 'shape': 6, 'on': 40, 'kiss': 1, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'discovering': 6, 'something': 6, 'be': 16, 'bedsheets': 3, 'fill': 2, 'hours': 2, 'stop': 1, 'bar': 1}\n", + "\n", + "keys = list(word_freq)\n", + "keys.sort()\n", + " \n", + "# Create an empty dictionary word_freq2\n", + "word_freq2 = {}\n", + "\n", + "# Use a FOR loop to iterate each value in keys. For each key iterated, find the corresponding value in word_freq and insert the key-value pair to word_freq2.\n", + "for value in keys:\n", + " word_freq2[value] = word_freq.get(value)\n", + "print(word_freq2)\n" ] }, { @@ -92,12 +111,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'conversation': 1, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'going': 1, 'one': 1, 'backseat': 1, 'friends': 1, 'take': 1, 'play': 1, 'okay': 1, 'begin': 1, 'over': 1, 'just': 1, 'are': 1, 'tell': 1, 'drinking': 1, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, 'can': 1, 'club': 1, 'it': 1, 'out': 1, 'chance': 1, 'first': 1, 'table': 1, 'thrifty': 1, 'driver': 1, 'slow': 1, 'dance': 1, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'leave': 1, 'at': 1, 'hand': 1, 'how': 1, 'eat': 1, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'let': 1, 'van': 1, 'shots': 1, 'place': 1, 'find': 1, 'kiss': 1, 'stop': 1, 'bar': 1, \"don't\": 2, 'mind': 2, 'know': 2, 'so': 2, 'start': 2, 'boy': 2, 'girl': 2, 'singing': 2, 'doing': 2, 'somebody': 2, 'handmade': 2, 'may': 2, 'that': 2, 'much': 2, 'grab': 2, 'was': 2, 'say': 2, 'waist': 2, 'want': 2, \"let's\": 2, 'not': 2, 'crazy': 2, 'go': 2, 'to': 2, 'fill': 2, 'hours': 2, 'push': 3, 'then': 3, 'put': 3, 'room': 3, 'magnet': 3, 'up': 3, 'pull': 3, 'last': 3, 'do': 3, 'smell': 3, 'although': 3, 'falling': 3, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'bedsheets': 3, 'talk': 4, 'too': 5, 'is': 5, 'every': 6, 'new': 6, 'follow': 6, 'brand': 6, 'of': 6, 'i': 6, 'day': 6, 'lead': 6, 'shape': 6, 'discovering': 6, 'something': 6, 'we': 7, 'a': 8, 'like': 10, 'me': 10, 'now': 11, 'baby': 14, 'you': 16, 'be': 16, 'body': 17, 'the': 18, 'your': 21, 'with': 22, \"i'm\": 23, 'and': 23, 'love': 25, 'in': 27, 'my': 33, 'come': 37, 'on': 40}\n" + ] + } + ], "source": [ - "# Your code here\n" + "import operator\n", + "\n", + "# Therefore, the steps to sort word_freq by value are:\n", + "\n", + "# 1. Using sorted and operator.itemgetter, obtain a list of tuples of the dict key-value pairs which is sorted on the value.\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "\n", + "# 2. Create an empty dictionary named word_freq2.\n", + "word_freq2 = {}\n", + "\n", + "# 3. Iterate the list of tuples. Insert each key-value pair into word_freq2 as an object.\n", + "for key, value in sorted_tups:\n", + " word_freq2[key] = value\n", + "print(word_freq2)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -116,7 +163,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.10.9" }, "nbTranslate": { "displayLangs": [ From 694c00fb860107cd212444ecf82f945151657cc1 Mon Sep 17 00:00:00 2001 From: Estherkii <123992666+Estherkii@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:50:08 +0200 Subject: [PATCH 2/2] upload --- your-code/challenge-1.ipynb | 23 +++++----- your-code/challenge-2.ipynb | 86 ++++++++++++++++++------------------- 2 files changed, 55 insertions(+), 54 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index e196578..e863af1 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -214,30 +214,33 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "4\n", - "4\n", - "Result is: True\n" + "The number of elements in tup1 and tup2 is: 8\n", + "Sets are both: 8\n" ] } ], "source": [ "#count the number of elements in tup1 and tup2\n", "\n", - "tup4 = len(tup1)\n", - "print(tup4)\n", - "tup5 = len(tup1)\n", - "print(tup5)\n", + "tup1= ['I', 'r', 'o', 'n']\n", + "tup2= ['h', 'a', 'c', 'k']\n", + "tup3 = ['I', 'r', 'o', 'n', 'h', 'a', 'c', 'k']\n", + "\n", + "sum1 = len(tup1)\n", + "sum2 = len(tup2)\n", + "sum12 = sum1 + sum2\n", + "print(\"The number of elements in tup1 and tup2 is:\", sum12)\n", "\n", "# check if sets are identical\n", - "res = tup4 == tup5\n", - "print(\"Result is: \", (res))" + "sum3 = len(tup3)\n", + "print(\"Sets are both:\", sum3)" ] }, { diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 3ef8f5d..77b0a11 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -47,14 +47,14 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[15.916644426710391, 78.94881937576783, 40.24477755723228, 34.14921518490173, 97.18007088115624, 85.50752756824687, 16.68730704672545, 30.618026818214616, 12.126654837790873, 37.2679714475359, 32.03629919498058, 53.187861654687715, 92.09370678987545, 12.481777397392024, 47.04517794525027, 81.80945487818069, 96.27038803031687, 72.53111309030287, 79.61880449686633, 73.28361121480732, 95.7909198995813, 82.53715729561554, 39.38819320499882, 23.342008223947673, 0.8383571856863892, 44.37676065758303, 51.17888640339291, 46.74727106342924, 24.53640775487438, 89.33398730501085, 81.93688380190332, 28.23592604298304, 34.18736279300535, 73.4439474392637, 53.074131156745366, 25.03375630262684, 41.23862412361197, 89.68160475447522, 77.23938435227859, 8.808652851836541, 63.38258498534428, 26.586171790889722, 50.7603135853296, 73.47231256181708, 87.29671767823893, 75.74163881528293, 18.862465302377117, 58.02653926183093, 16.244707935313485, 77.98253520171474, 44.22195361329242, 46.358608299737845, 63.990061460083304, 32.63299289743994, 23.34862027079415, 21.499452455863256, 86.15252155802084, 62.56598245210338, 58.95386924514021, 22.991164290777267, 34.7346410726613, 51.71508293499969, 66.55885224253376, 7.582864682545932, 0.47253432057959843, 93.69885799443958, 20.835210788722247, 54.984203571312406, 68.46171052251607, 36.814622495619496, 71.36419500412565, 6.716642885024305, 76.73635231415123, 47.06595040761169, 59.44605325786934, 92.3353267216173, 15.994156058594955, 73.90455783123426, 20.37348171231169, 70.9981307104103, 46.62028529972052]\n" + "[61, 41, 5, 67, 90, 62, 89, 0, 10, 39, 49, 91, 63, 55, 27, 53, 88, 46, 64, 21, 17, 40, 96, 68, 52, 97, 72, 38, 20, 81, 65, 44, 58, 15, 59, 8, 57, 33, 28, 83, 22, 43, 2, 87, 19, 95, 76, 75, 78, 13, 3, 50, 42, 56, 9, 77, 71, 70, 92, 69, 24, 47, 7, 86, 37, 34, 30, 51, 94, 99, 16, 1, 45, 4, 98, 18, 11, 73, 60, 93]\n" ] } ], @@ -62,18 +62,13 @@ "# In the cell below, create a list named sample_list_1 with 80 random values.\n", "# Requirements:\n", "# Each value is an integer falling between 0 and 100.\n", - "min_value = 0\n", - "max_value = 100\n", - "\n", - "# Create a list with 80 random values, each value must be unique.\n", - "sample_list_1 = [random.uniform(min_value, max_value) for _ in range(80)]\n", + "# Each value in the list must be unique\n", "\n", - "#each value must be unique\n", - "if new_value not in sample_list_1:\n", - " sample_list_1.append(new_value)\n", + "import random\n", + "sample_list_1 = random.sample(range(100), k=80)\n", + "print(sample_list_1)\n", "\n", - "# Print the list\n", - "print(sample_list_1)\n" + "# CORRECCIONES: Has usado random.uniform(a, b) por lo que te genera una lista de valores aloatorios con floats. En el enunciado especifica que deben ser integers por lo deberías usar random.sample(a, b) .Al haber puesto floats, los siguientes ejercicios del lab relacionados están bien planteado a nivel de código pero los resultados no son correctos. 🫠" ] }, { @@ -85,14 +80,14 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Length of set1: 81\n" + "Length of set1: 80\n" ] } ], @@ -101,7 +96,7 @@ "\n", "print(\"Length of set1:\", len(set1))\n", "\n", - "# No, it is 81" + "# Yes, it is 80" ] }, { @@ -120,14 +115,14 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[68, 24, 55, 67, 62, 53, 38, 72, 18, 12, 33, 17, 38, 12, 77, 7, 56, 14, 17, 80, 57, 19, 44, 53, 21, 72, 69, 23, 32, 71, 27, 18, 43, 57, 69, 63, 9, 27, 15, 28, 83, 86, 92, 22, 78, 41, 65, 33, 4, 13, 80, 81, 32, 18, 31, 96, 95, 94, 64, 34, 21, 10, 45, 45, 33, 32, 37, 96, 2, 55, 56, 36, 68, 49, 20, 29, 14, 8, 42, 89]\n" + "[38, 32, 16, 55, 14, 69, 17, 94, 35, 81, 33, 15, 9, 86, 44, 38, 23, 1, 5, 84, 35, 96, 11, 44, 10, 55, 90, 59, 13, 12, 85, 84, 71, 23, 43, 68, 9, 20, 0, 23, 15, 85, 60, 28, 31, 99, 21, 8, 3, 65, 12, 8, 45, 72, 58, 95, 71, 70, 68, 45, 16, 66, 91, 28, 40, 74, 33, 45, 72, 31, 42, 86, 15, 79, 39, 10, 7, 49, 59, 57]\n" ] } ], @@ -159,14 +154,14 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Length of set2: 58\n" + "Length of set2: 55\n" ] } ], @@ -175,7 +170,7 @@ "\n", "print(\"Length of set2:\", len(set2))\n", "\n", - "# No, it is 58" + "# No, it is 55" ] }, { @@ -187,14 +182,14 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Elements in set1 named set3: {0.8383571856863892, 0.47253432057959843, 6.716642885024305, 7.582864682545932, 8.808652851836541, 12.126654837790873, 12.481777397392024, 15.916644426710391, 16.68730704672545, 16.244707935313485, 18.862465302377117, 15.994156058594955, 20.835210788722247, 21.499452455863256, 22.991164290777267, 23.342008223947673, 24.53640775487438, 25.03375630262684, 26.586171790889722, 23.34862027079415, 28.23592604298304, 20.37348171231169, 30.618026818214616, 32.03629919498058, 32.63299289743994, 34.14921518490173, 34.18736279300535, 34.7346410726613, 37.2679714475359, 36.814622495619496, 39.38819320499882, 40.24477755723228, 41.23862412361197, 44.37676065758303, 44.22195361329242, 46.74727106342924, 47.04517794525027, 46.358608299737845, 47.06595040761169, 50.7603135853296, 51.17888640339291, 51.71508293499969, 53.187861654687715, 53.074131156745366, 54.984203571312406, 58.02653926183093, 58.95386924514021, 59.44605325786934, 62.56598245210338, 63.38258498534428, 63.990061460083304, 66.55885224253376, 68.46171052251607, 70.9981307104103, 71.36419500412565, 72.53111309030287, 73.28361121480732, 73.4439474392637, 73.47231256181708, 75.74163881528293, 77.23938435227859, 78.94881937576783, 79.61880449686633, 77.98253520171474, 81.80945487818069, 82.53715729561554, 81.93688380190332, 76.73635231415123, 85.50752756824687, 86.15252155802084, 87.29671767823893, 89.33398730501085, 89.68160475447522, 92.09370678987545, 93.69885799443958, 92.3353267216173, 95.7909198995813, 96.27038803031687, 97.18007088115624, 46.62028529972052, 73.90455783123426}\n" + "Elements in set1 named set3: {2, 4, 6, 18, 19, 22, 24, 25, 29, 30, 34, 36, 37, 41, 46, 47, 48, 51, 52, 53, 54, 56, 61, 62, 64, 67, 76, 78, 80, 82, 87, 88, 89, 92, 97}\n" ] } ], @@ -213,14 +208,14 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Elements in set2 named set4: {2, 4, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 29, 31, 32, 33, 34, 36, 37, 38, 41, 42, 43, 44, 45, 49, 53, 55, 56, 57, 62, 63, 64, 65, 67, 68, 69, 71, 72, 77, 78, 80, 81, 83, 86, 89, 92, 94, 95, 96}\n" + "Elements in set2 named set4: {72, 9, 74, 11, 91, 45, 21, 55, 57, 59}\n" ] } ], @@ -239,7 +234,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -288,7 +283,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -297,9 +292,9 @@ "text": [ "Length of set1: 81\n", "Length of set2: 58\n", - "Length of set3: 23\n", - "Length of set4: 58\n", - "Length of set5: -58\n" + "Length of set3: 42\n", + "Length of set4: 10\n", + "Length of set5: -12\n" ] } ], @@ -329,7 +324,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -345,14 +340,14 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{0.8383571856863892, 0.47253432057959843, 6.716642885024305, 7.582864682545932, 8.808652851836541, 7, 8, 12.126654837790873, 12.481777397392024, 12, 15.916644426710391, 16.68730704672545, 16.244707935313485, 18.862465302377117, 15.994156058594955, 20.835210788722247, 21.499452455863256, 22.991164290777267, 23.342008223947673, 24.53640775487438, 25.03375630262684, 26.586171790889722, 23.34862027079415, 28.23592604298304, 20.37348171231169, 30.618026818214616, 22, 32.03629919498058, 32.63299289743994, 34.14921518490173, 34.18736279300535, 34.7346410726613, 37.2679714475359, 36.814622495619496, 39.38819320499882, 40.24477755723228, 41.23862412361197, 34, 36, 44.37676065758303, 44.22195361329242, 46.74727106342924, 47.04517794525027, 46.358608299737845, 47.06595040761169, 50.7603135853296, 51.17888640339291, 51.71508293499969, 53.187861654687715, 53.074131156745366, 54.984203571312406, 53, 58.02653926183093, 58.95386924514021, 59.44605325786934, 62.56598245210338, 63.38258498534428, 63.990061460083304, 62, 66.55885224253376, 63, 68.46171052251607, 68, 70.9981307104103, 71.36419500412565, 72.53111309030287, 73.28361121480732, 73.4439474392637, 73.47231256181708, 75.74163881528293, 77.23938435227859, 78.94881937576783, 79.61880449686633, 77.98253520171474, 81.80945487818069, 82.53715729561554, 81.93688380190332, 76.73635231415123, 85.50752756824687, 86.15252155802084, 87.29671767823893, 81, 89.33398730501085, 89.68160475447522, 18, 92.09370678987545, 93.69885799443958, 92.3353267216173, 95.7909198995813, 96.27038803031687, 97.18007088115624, 89, 92, 95, 20, 96, 21, 23, 24, 28, 32, 37, 41, 44, 46.62028529972052, 71, 72, 73.90455783123426, 15, 77, 78, 86}\n" + "{2, 4, 6, 7, 8, 12, 15, 18, 19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 32, 34, 36, 37, 41, 44, 46, 47, 48, 51, 52, 53, 54, 56, 61, 62, 63, 64, 67, 68, 71, 72, 76, 77, 78, 80, 81, 82, 86, 87, 88, 89, 92, 95, 96, 97}\n" ] } ], @@ -372,7 +367,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -399,7 +394,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 22, "metadata": {}, "outputs": [ { @@ -407,7 +402,7 @@ "output_type": "stream", "text": [ "set1 does not contain set2\n", - "set1 contains set3\n" + "set3 does not contain set1\n" ] } ], @@ -437,14 +432,14 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Sets are equal\n" + "Sets are not equal\n" ] } ], @@ -471,21 +466,24 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Removed first element: 7.582864682545932\n" + "{7.582864682545932, 8.808652851836541, 12.126654837790873, 12.481777397392024, 15.916644426710391, 16.68730704672545, 16.244707935313485, 18.862465302377117, 15.994156058594955, 20.835210788722247, 21.499452455863256, 22.991164290777267, 23.342008223947673, 24.53640775487438, 25.03375630262684, 26.586171790889722, 23.34862027079415, 28.23592604298304, 20.37348171231169, 30.618026818214616, 32.03629919498058, 32.63299289743994, 34.14921518490173, 34.18736279300535, 34.7346410726613, 37.2679714475359, 36.814622495619496, 39.38819320499882, 40.24477755723228, 41.23862412361197, 44.37676065758303, 44.22195361329242, 46.74727106342924, 47.04517794525027, 46.358608299737845, 47.06595040761169, 50.7603135853296, 51.17888640339291, 51.71508293499969, 53.187861654687715, 53.074131156745366, 54.984203571312406, 58.02653926183093, 58.95386924514021, 59.44605325786934, 62.56598245210338, 63.38258498534428, 63.990061460083304, 66.55885224253376, 68.46171052251607, 70.9981307104103, 71.36419500412565, 72.53111309030287, 73.28361121480732, 73.4439474392637, 73.47231256181708, 75.74163881528293, 77.23938435227859, 78.94881937576783, 79.61880449686633, 77.98253520171474, 81.80945487818069, 82.53715729561554, 81.93688380190332, 76.73635231415123, 85.50752756824687, 86.15252155802084, 87.29671767823893, 89.33398730501085, 89.68160475447522, 92.09370678987545, 93.69885799443958, 92.3353267216173, 95.7909198995813, 96.27038803031687, 97.18007088115624, 46.62028529972052, 73.90455783123426}\n" ] } ], "source": [ - "if len(set1) > 0:\n", - " removed_element = set1.pop()\n", - " print(\"Removed first element:\", removed_element)" + "#CORRECCIONES: The pop method removes an element randomly, thus we could convert it to list to pop the first element.\n", + "\n", + "set1_list = list(set1)\n", + "set1_list.pop(0)\n", + "set1_list_pop = set(set1_list) # And we convert it back to a set\n", + "print(set1_list_pop)" ] }, { @@ -501,14 +499,14 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Remaining elements: {8.808652851836541, 12.126654837790873, 12.481777397392024, 15.916644426710391, 16.68730704672545, 16.244707935313485, 18.862465302377117, 15.994156058594955, 20.835210788722247, 21.499452455863256, 22.991164290777267, 23.342008223947673, 24.53640775487438, 25.03375630262684, 26.586171790889722, 23.34862027079415, 28.23592604298304, 20.37348171231169, 30.618026818214616, 32.03629919498058, 32.63299289743994, 34.14921518490173, 34.18736279300535, 34.7346410726613, 37.2679714475359, 36.814622495619496, 39.38819320499882, 40.24477755723228, 41.23862412361197, 44.37676065758303, 44.22195361329242, 46.74727106342924, 47.04517794525027, 46.358608299737845, 47.06595040761169, 50.7603135853296, 51.17888640339291, 51.71508293499969, 53.187861654687715, 53.074131156745366, 54.984203571312406, 58.02653926183093, 58.95386924514021, 59.44605325786934, 62.56598245210338, 63.38258498534428, 63.990061460083304, 66.55885224253376, 68.46171052251607, 70.9981307104103, 71.36419500412565, 72.53111309030287, 73.28361121480732, 73.4439474392637, 73.47231256181708, 75.74163881528293, 77.23938435227859, 78.94881937576783, 79.61880449686633, 77.98253520171474, 81.80945487818069, 82.53715729561554, 81.93688380190332, 76.73635231415123, 85.50752756824687, 86.15252155802084, 87.29671767823893, 89.33398730501085, 89.68160475447522, 92.09370678987545, 93.69885799443958, 92.3353267216173, 95.7909198995813, 96.27038803031687, 97.18007088115624, 46.62028529972052, 73.90455783123426}\n" + "Remaining elements: {0.47253432057959843, 6.716642885024305, 7.582864682545932, 8.808652851836541, 12.126654837790873, 12.481777397392024, 15.916644426710391, 16.68730704672545, 16.244707935313485, 18.862465302377117, 15.994156058594955, 20.835210788722247, 21.499452455863256, 22.991164290777267, 23.342008223947673, 24.53640775487438, 25.03375630262684, 26.586171790889722, 23.34862027079415, 28.23592604298304, 20.37348171231169, 30.618026818214616, 32.03629919498058, 32.63299289743994, 34.14921518490173, 34.18736279300535, 34.7346410726613, 37.2679714475359, 36.814622495619496, 39.38819320499882, 40.24477755723228, 41.23862412361197, 44.37676065758303, 44.22195361329242, 46.74727106342924, 47.04517794525027, 46.358608299737845, 47.06595040761169, 50.7603135853296, 51.17888640339291, 51.71508293499969, 53.187861654687715, 53.074131156745366, 54.984203571312406, 58.02653926183093, 58.95386924514021, 59.44605325786934, 62.56598245210338, 63.38258498534428, 63.990061460083304, 66.55885224253376, 68.46171052251607, 70.9981307104103, 71.36419500412565, 72.53111309030287, 73.28361121480732, 73.4439474392637, 73.47231256181708, 75.74163881528293, 77.23938435227859, 78.94881937576783, 79.61880449686633, 77.98253520171474, 81.80945487818069, 82.53715729561554, 81.93688380190332, 76.73635231415123, 85.50752756824687, 86.15252155802084, 87.29671767823893, 89.33398730501085, 89.68160475447522, 92.09370678987545, 93.69885799443958, 92.3353267216173, 95.7909198995813, 96.27038803031687, 97.18007088115624, 73.90455783123426, 46.62028529972052}\n" ] } ],