From db2ec067fd259752c22428cb93c469c67886304c Mon Sep 17 00:00:00 2001 From: PedroFariaBlanc <82642165+PedroFariaBlanc@users.noreply.github.com> Date: Tue, 18 Apr 2023 15:40:23 +0100 Subject: [PATCH] Tuple Set Dict Lab Done --- your-code/challenge-1.ipynb | 197 +++++++++++++--- your-code/challenge-2.ipynb | 444 +++++++++++++++++++++++++++++------- your-code/challenge-3.ipynb | 158 +++++++++++-- 3 files changed, 665 insertions(+), 134 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 2e59d77..49b2c5f 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "tup = (\"I\",)" ] }, { @@ -33,11 +35,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "type(tup)" ] }, { @@ -55,13 +70,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Your code here\n", "\n", - "# Your explanation here\n" + "tup = [\"I\",]\n", + "tup.append(\"r\")\n", + "tup.append(\"o\")\n", + "tup.append(\"n\")\n", + "tup.append(\"h\")\n", + "tup.append(\"a\")\n", + "tup.append(\"c\")\n", + "tup.append(\"k\")\n", + "\n", + "\n", + "# Your explanation here\n", + "\n", + "#Tuples are immutable, which means their values cannot be changed after they are created.\n", + "#However, we can append one element at a time." ] }, { @@ -79,13 +107,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Your code here\n", "\n", - "# Your explanation here\n" + "# Your explanation here\n", + "\n", + "# Tuples are a data type which is actually immutable, this means that they cannot be changed once created." ] }, { @@ -103,11 +133,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tup1: ('I', 'r', 'o', 'n')\n", + "tup2: ('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "# define the tuple once again\n", + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "\n", + "# since you can't use the method split() in tuples, reassign new variables called tup1 and tup2\n", + "tup1 = tup[:4]\n", + "tup2 = tup[-4:]\n", + "\n", + "# now print\n", + "print(\"tup1:\", tup1)\n", + "print(\"tup2:\", tup2)\n" ] }, { @@ -121,11 +171,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tup3 and tup are equal\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "tup3 = tup1 + tup2\n", + "\n", + "tup3\n", + "\n", + "if tup3 == tup:\n", + " print(\"tup3 and tup are equal\")" ] }, { @@ -137,11 +202,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of elements in tup1: 4\n", + "Number of elements in tup2: 4\n", + "Number of elements in tup3: 8\n", + "True\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "count_tup1 = len(tup1)\n", + "count_tup2 = len(tup2)\n", + "count_tup3 = len(tup3)\n", + "\n", + "print(\"Number of elements in tup1:\", count_tup1)\n", + "print(\"Number of elements in tup2:\", count_tup2)\n", + "print(\"Number of elements in tup3:\", count_tup3)\n", + "\n", + "if count_tup1 + count_tup2 == count_tup3:\n", + " print(\"True\")" ] }, { @@ -153,11 +240,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "index_of_h = tup3.index(\"h\")\n", + "\n", + "print(index_of_h)" ] }, { @@ -177,11 +276,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "False\n", + "False\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "for letter in letters:\n", + " if letter in tup3:\n", + " print(True)\n", + " else:\n", + " print(False)" ] }, { @@ -195,11 +314,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The letter 'a' appears 1 time(s) in tup3\n", + "The letter 'b' appears 0 time(s) in tup3\n", + "The letter 'c' appears 1 time(s) in tup3\n", + "The letter 'd' appears 0 time(s) in tup3\n", + "The letter 'e' appears 0 time(s) in tup3\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "tup3 = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "for letter in letters:\n", + " count = 0\n", + " for char in tup3:\n", + " if char == letter:\n", + " count += 1\n", + " print(f\"The letter '{letter}' appears {count} time(s) in tup3\")\n" ] } ], @@ -219,7 +360,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.11.2" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index cb3a3e0..49aba7b 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": 30, "metadata": {}, "outputs": [], "source": [ @@ -38,11 +38,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[45, 49, 23, 63, 36, 24, 100, 67, 48, 3, 44, 27, 94, 54, 29, 10, 0, 80, 34, 32, 18, 16, 60, 71, 81, 38, 30, 6, 53, 97, 50, 35, 51, 37, 61, 84, 41, 66, 56, 43, 9, 25, 20, 98, 95, 52, 57, 69, 72, 83, 13, 65, 15, 68, 78, 87, 14, 91, 21, 11, 75, 76, 64, 19, 58, 70, 82, 8, 7, 2, 55, 42, 77, 31, 96, 5, 89, 93, 26, 92]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "sample_list_1 = random.sample(range(101), 80)\n", + "print(sample_list_1)" ] }, { @@ -54,11 +65,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "{0, 2, 3, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 18, 19, 20, 21, 23, 24, 25, 26, 27, 29, 30, 31, 32, 34, 35, 36, 37, 38, 41, 42, 43, 44, 45, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 75, 76, 77, 78, 80, 81, 82, 83, 84, 87, 89, 91, 92, 93, 94, 95, 96, 97, 98, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "set1 = set(sample_list_1)\n", + "print(len(set1)) # the length is 80\n", + "\n", + "print(set1)" ] }, { @@ -77,11 +102,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[8, 73, 24, 96, 28, 90, 43, 28, 6, 77, 50, 31, 66, 55, 22, 50, 58, 65, 86, 76, 91, 12, 99, 32, 18, 80, 17, 5, 33, 59, 78, 14, 90, 73, 23, 90, 78, 2, 97, 95, 79, 92, 100, 93, 73, 62, 99, 14, 100, 26, 65, 0, 3, 68, 35, 4, 25, 55, 35, 30, 3, 4, 91, 61, 38, 95, 75, 50, 92, 53, 58, 36, 56, 72, 59, 16, 90, 37, 29, 21]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "import random\n", + "\n", + "sample_list_2 = []\n", + "for i in range(80):\n", + " sample_list_2.append(random.randint(0, 100))\n", + "\n", + "print(sample_list_2)\n" ] }, { @@ -93,11 +134,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "58\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "set2 = set(sample_list_2)\n", + "print(len(set2)) # the length is now 58" ] }, { @@ -109,11 +161,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{7, 9, 10, 11, 13, 15, 19, 20, 27, 34, 41, 42, 44, 45, 48, 49, 51, 52, 54, 57, 60, 63, 64, 67, 69, 70, 71, 81, 82, 83, 84, 87, 89, 94, 98}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "set3 = set1.difference(set2)\n", + "print(set3)" ] }, { @@ -125,11 +188,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{33, 99, 4, 73, 12, 79, 17, 22, 86, 90, 59, 28, 62}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "set4 = set2.difference(set1)\n", + "print(set4)" ] }, { @@ -141,11 +215,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 5, 6, 8, 14, 16, 18, 21, 23, 24, 25, 26, 29, 30, 31, 32, 35, 36, 37, 38, 43, 50, 53, 55, 56, 58, 61, 65, 66, 68, 72, 75, 76, 77, 78, 80, 91, 92, 93, 95, 96, 97, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "set5 = set1.intersection(set2)\n", + "print(set5)" ] }, { @@ -157,11 +242,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "set6 = set()" ] }, { @@ -173,11 +260,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 18, 19, 20, 21, 23, 24, 25, 26, 27, 29, 30, 31, 32, 34, 35, 36, 37, 38, 41, 42, 43, 44, 45, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 75, 76, 77, 78, 80, 81, 82, 83, 84, 87, 89, 91, 92, 93, 94, 95, 96, 97, 98, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "set6.update(set3)\n", + "set6.update(set5)\n", + "\n", + "print(set6)" ] }, { @@ -189,11 +289,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set1 and set6 are equal\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "if set1 == set6:\n", + " print(\"set1 and set6 are equal\")\n", + "else:\n", + " print(\"set1 and set6 are not equal\")\n" ] }, { @@ -205,11 +318,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set2 is not a subset of set1\n", + "set3 is a subset of set1\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "if set2.issubset(set1):\n", + " print(\"set2 is a subset of set1\")\n", + "else:\n", + " print(\"set2 is not a subset of set1\")\n", + "\n", + "if set3.issubset(set1):\n", + " print(\"set3 is a subset of set1\")\n", + "else:\n", + " print(\"set3 is not a subset of set1\")" ] }, { @@ -223,11 +355,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 41, 42, 43, 44, 45, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}\n", + "{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 41, 42, 43, 44, 45, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}\n", + "set7 and set8 are equal\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "set7 = set3.union(set4, set5)\n", + "print(set7)\n", + "\n", + "set8 = set1.union(set2)\n", + "print(set8)\n", + "\n", + "if set7 == set8:\n", + " print(\"set7 and set8 are equal\")\n", + "else:\n", + " print(\"set7 and set8 are not equal\")" ] }, { @@ -239,11 +392,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Removed element: 0\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "first_element = set1.pop()\n", + "print(\"Removed element:\", first_element)\n" ] }, { @@ -259,11 +423,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Remaining elements in set 1: {2, 3, 5, 6, 7, 8, 10, 13, 14, 15, 16, 18, 20, 23, 24, 25, 26, 27, 30, 32, 34, 35, 36, 37, 38, 42, 43, 44, 45, 48, 50, 52, 53, 54, 55, 56, 57, 58, 60, 63, 64, 65, 66, 67, 68, 70, 72, 75, 76, 77, 78, 80, 82, 83, 84, 87, 92, 93, 94, 95, 96, 97, 98, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\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", + "for element in list_to_remove:\n", + " if element in set1:\n", + " set1.remove(element)\n", + "\n", + "print(\"Remaining elements in set 1:\", set1)\n" ] }, { @@ -278,7 +458,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ @@ -304,11 +484,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "father_list = ['garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions', 'gums', 'flour', 'cucumber', 'watermelon', 'yogurt', 'garlic']\n", + "father_set = set(father_list)\n", + "\n", + "mother_list = []\n", + "mother_set = set(mother_list)\n", + "\n", + "Jo_list = ['blueberries', 'sugar', 'watermelon', 'gums', 'tomatoes', 'yogurt', 'juice', 'milk', 'onions', 'garlic', 'cucumber', 'sugar', 'blueberries', 'gums', 'yogurt']\n", + "Jo_set = set(Jo_list)\n", + "\n", + "Carlos_list = ['tomatoes', 'water', 'onions', 'blueberries', 'garlic', 'flour', 'cherries', 'tomatoes', 'onions', 'water', 'tomatoes', 'toilet paper']\n", + "Carlos_set = set(Carlos_list)\n", + "\n", + "Mattia_list = []\n", + "Mattia_set = set(Mattia_list)\n" ] }, { @@ -322,11 +517,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'juice', 'flour', 'cucumber', 'gums', 'toilet paper', 'milk', 'cherries', 'water', 'yogurt', 'watermelon', 'sugar'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "# what both Carlos and Jo want\n", + "Carlos_set = set(Carlos_list)\n", + "Jo_set = set(Jo_list)\n", + "common_items = Carlos_set.intersection(Jo_set)\n", + "\n", + "# create a set for what Mattia wants\n", + "Mattia_set = set(Carlos_set.symmetric_difference(Jo_set))\n", + "\n", + "# remove common items between Carlos and Jo from Mattia\n", + "Mattia_set = Mattia_set.difference(common_items)\n", + "\n", + "\n", + "print(Mattia_set)\n", + "\n" ] }, { @@ -338,9 +556,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['toilet paper', 'shampoo', 'milk', 'milk', 'onions', 'vinegar', 'vinegar', 'water', 'gums', 'sugar']\n" + ] + } + ], "source": [ "import random\n", "items = ['milk', 'water', 'chocolate', 'blueberries', 'shampoo', 'flour', 'bread', 'sugar', 'watermelon', 'vinegar', 'tomatoes', 'yogurt', 'juice', 'gums', 'onions', 'garlic', 'cucumber', 'mushrooms', 'toilet paper', 'oranges', 'deodorant', 'cherries']\n", @@ -352,11 +578,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "mother_set = set(mother_list)\n", + "mother = list(mother_set)\n" ] }, { @@ -373,11 +602,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "father_set = set(father_list)\n", + "mother_set = set(mother)\n", + "Jo_set = set(Jo_list)\n", + "Carlos_set = set(Carlos_list)\n", + "sets_list = [father_set, mother_set, Jo_set, Carlos_set]\n", + "\n", + "for s in sets_list:\n", + " s.discard('toilet paper')\n" ] }, { @@ -389,11 +627,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "father_set = set(father_list)\n", + "mother_set = set(mother)\n", + "Jo_set = set(Jo_list)\n", + "Carlos_set = set(Carlos_list)\n", + "Mattia_set = set(Carlos_set.symmetric_difference(Jo_set)).difference(Carlos_set.intersection(Jo_set))\n", + "\n", + "all_items_set = father_set.union(mother_set, Jo_set, Carlos_set, Mattia_set)\n" ] }, { @@ -407,11 +653,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "father_list = []\n", + "for i in range(5):\n", + " item = all_items_set.pop()\n", + " father_list.append(item)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['juice', 'garlic', 'blueberries', 'toilet paper', 'shampoo']\n" + ] + } + ], + "source": [ + "print(father_list)" ] } ], @@ -431,7 +699,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.11.2" } }, "nbformat": 4, diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 7ab8ea5..1f9cc4a 100644 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -49,11 +49,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "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" + "# Your code here\n", + "\n", + "# extract the keys of word_freq and convert to a list\n", + "keys = list(word_freq.keys())\n", + "\n", + "# sort the keys list\n", + "keys.sort()\n", + "\n", + "# create an empty dictionary\n", + "word_freq2 = {}\n", + "\n", + "# iterate over the sorted keys and insert into word_freq2\n", + "for key in keys:\n", + " word_freq2[key] = word_freq[key]\n", + "\n", + "# print out word_freq2\n", + "print(word_freq2)" ] }, { @@ -90,11 +114,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "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" + "# Your code here\n", + "\n", + "import operator\n", + "\n", + "# using sorted and operator\n", + "sorted_tups = sorted(word_freq.items(), key = operator.itemgetter(1))\n", + "\n", + "# creating an empty dictionary\n", + "word_freq2 = {}\n", + "\n", + "# iterating list of tuples\n", + "for tup in sorted_tups:\n", + " word_freq2[tup[0]] = tup[1]\n", + "\n", + "\n", + "print(word_freq2)\n" ] }, { @@ -110,7 +157,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -132,11 +179,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "0 love 25\n", + "1 conversation 1\n", + "2 every 6\n", + "3 we're 1\n", + "4 plate 1\n", + ".. ... ...\n", + "135 bedsheets 3\n", + "136 fill 2\n", + "137 hours 2\n", + "138 stop 1\n", + "139 bar 1\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "df = pd.DataFrame(list(word_freq.items()), columns=['word', 'freq'])\n", + "\n", + "print(df)" ] }, { @@ -150,11 +222,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "120 a 8\n", + "109 about 1\n", + "43 all 1\n", + "96 although 3\n", + "72 and 23\n", + ".. ... ...\n", + "128 were 3\n", + "41 where 1\n", + "54 with 22\n", + "15 you 16\n", + "97 your 21\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "df = df.sort_values('word', ascending=True)\n", + "\n", + "print(df)" ] }, { @@ -166,11 +263,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "114 let 1\n", + "87 trust 1\n", + "11 man 1\n", + "45 make 1\n", + "46 lover 1\n", + ".. ... ...\n", + "0 love 25\n", + "65 in 27\n", + "121 my 33\n", + "56 come 37\n", + "126 on 40\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "df = df.sort_values('freq', ascending=True)\n", + "\n", + "print(df)" ] } ], @@ -190,7 +312,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.11.2" } }, "nbformat": 4,