From c667aadc8046424f73db43f20fe5200a89ed8c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20S=C3=A1nchez?= Date: Tue, 18 Apr 2023 23:40:51 +0100 Subject: [PATCH] C:\Users\jsctr\Dropbox\IronHack\Labs\Week_1\lab-tuple-set-dict --- your-code/challenge-1.ipynb | 202 ++++++++++-- your-code/challenge-2.ipynb | 624 ++++++++++++++++++++++++++++++------ your-code/challenge-3.ipynb | 163 +++++++++- 3 files changed, 845 insertions(+), 144 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 2e59d77..a2f6fef 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1,)\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup = (1,)\n", + "print(mytuple)\n" ] }, { @@ -33,14 +43,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "type(tup)" ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -55,13 +78,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "unterminated string literal (detected at line 3) (29588915.py, line 3)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[15], line 3\u001b[1;36m\u001b[0m\n\u001b[1;33m tup.append(\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k',)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unterminated string literal (detected at line 3)\n" + ] + } + ], "source": [ "# Your code here\n", "\n", - "# Your explanation here\n" + "tup.append(\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k',)\n", + "\n", + "# Your explanation here\n", + "\n", + "No because once a tuple is created, it is inmutable" ] }, { @@ -79,13 +115,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "# Your code here\n", "\n", - "# Your explanation here\n" + "tup\n", + "\n", + "# Your explanation here\n", + "\n", + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "\n", + "#Values inside tuple weren't changed, the tuple itself was changed entirely. It's a different tuple with the same name\n", + "\n", + "\n" ] }, { @@ -103,11 +147,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n')\n", + "('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup\n", + "\n", + "tup1 = tup[0:4]\n", + "tup2 = tup[-4:]\n", + "print(tup1)\n", + "print(tup2)" ] }, { @@ -121,11 +180,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "tup3 = tup1 + tup2\n", + "tup3\n" ] }, { @@ -137,11 +210,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "targetlen = len(tup3)\n", + "\n", + "count1 = len(tup1)\n", + "count2 = len(tup2)\n", + "count3 = count1 + count2\n", + "count3 == targetlen\n", + "\n" ] }, { @@ -153,11 +245,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "index_h = tup3.index(\"h\")\n", + "index_h\n" ] }, { @@ -177,11 +282,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "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)\n" ] }, { @@ -195,11 +320,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a': 1, 'b': 0, 'c': 1, 'd': 0, 'e': 0}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "mydict = {}\n", + "\n", + "for letter in letters:\n", + " count = tup3.count(letter)\n", + " mydict[letter] = count\n", + "\n", + "print(mydict)\n", + " " ] } ], @@ -219,7 +361,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index cb3a3e0..882eed9 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": 4, "metadata": {}, "outputs": [], "source": [ @@ -38,11 +38,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[71, 53, 30, 86, 85, 47, 32, 99, 52, 84]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "sample_list_1 = random.sample(range(100), 80)\n", + "print(sample_list_1[:10])\n" ] }, { @@ -54,11 +64,105 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{0,\n", + " 1,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 11,\n", + " 12,\n", + " 13,\n", + " 15,\n", + " 17,\n", + " 18,\n", + " 19,\n", + " 20,\n", + " 21,\n", + " 22,\n", + " 23,\n", + " 24,\n", + " 26,\n", + " 27,\n", + " 28,\n", + " 29,\n", + " 30,\n", + " 32,\n", + " 35,\n", + " 36,\n", + " 37,\n", + " 39,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 43,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 49,\n", + " 51,\n", + " 52,\n", + " 53,\n", + " 54,\n", + " 55,\n", + " 57,\n", + " 59,\n", + " 60,\n", + " 61,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 67,\n", + " 68,\n", + " 69,\n", + " 70,\n", + " 71,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 76,\n", + " 77,\n", + " 79,\n", + " 80,\n", + " 81,\n", + " 83,\n", + " 84,\n", + " 85,\n", + " 86,\n", + " 87,\n", + " 88,\n", + " 90,\n", + " 92,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97,\n", + " 98,\n", + " 99}" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1 = set(sample_list_1)\n", + "set1\n", + "\n", + "# (yes, length and order is the same)" ] }, { @@ -77,11 +181,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[95, 53, 74, 42, 0, 45, 9, 21, 36, 20, 41, 6, 0, 87, 98, 13, 9, 69, 93, 37, 27, 34, 34, 48, 31, 63, 14, 46, 73, 43, 22, 96, 25, 20, 6, 90, 64, 50, 40, 90, 59, 99, 89, 42, 54, 31, 47, 26, 72, 93, 71, 61, 31, 79, 35, 14, 80, 20, 44, 49, 58, 5, 37, 50, 10, 83, 0, 5, 19, 27, 93, 83, 23, 31, 88, 32, 35, 8, 9, 10]\n" + ] + } + ], "source": [ - "# Your code here\n" + "import random\n", + "\n", + "sample_list_2 = []\n", + "\n", + "for a in range(80):\n", + " value = random.randint(0, 100)\n", + " sample_list_2.append(value)\n", + "\n", + "print(sample_list_2)" ] }, { @@ -93,11 +213,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 5, 6, 8, 9, 10, 13, 14, 19, 20, 21, 22, 23, 25, 26, 27, 31, 32, 34, 35, 36, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 53, 54, 58, 59, 61, 63, 64, 69, 71, 72, 73, 74, 79, 80, 83, 87, 88, 89, 90, 93, 95, 96, 98, 99}\n", + "57\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set2 = set(sample_list_2)\n", + "print(set2)\n", + "\n", + "#length?\n", + "\n", + "print(len(set2))" ] }, { @@ -109,11 +244,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 32, 35, 36, 37, 39, 40, 41, 42, 43, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 57, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 76, 77, 79, 80, 81, 83, 84, 85, 86, 87, 88, 90, 92, 94, 95, 96, 97, 98, 99}\n", + "{0, 5, 6, 8, 9, 10, 13, 14, 19, 20, 21, 22, 23, 25, 26, 27, 31, 32, 34, 35, 36, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 53, 54, 58, 59, 61, 63, 64, 69, 71, 72, 73, 74, 79, 80, 83, 87, 88, 89, 90, 93, 95, 96, 98, 99}\n", + "{1, 3, 4, 11, 12, 15, 17, 18, 24, 28, 29, 30, 39, 51, 52, 55, 57, 60, 62, 65, 67, 68, 70, 76, 77, 81, 84, 85, 86, 92, 94, 97}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(set1)\n", + "print(set2)\n", + "\n", + "set3 = set1 - set2\n", + "\n", + "print(set3)" ] }, { @@ -125,11 +276,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": [ + "{0, 1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 32, 35, 36, 37, 39, 40, 41, 42, 43, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 57, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 76, 77, 79, 80, 81, 83, 84, 85, 86, 87, 88, 90, 92, 94, 95, 96, 97, 98, 99}\n", + "{0, 5, 6, 8, 9, 10, 13, 14, 19, 20, 21, 22, 23, 25, 26, 27, 31, 32, 34, 35, 36, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 53, 54, 58, 59, 61, 63, 64, 69, 71, 72, 73, 74, 79, 80, 83, 87, 88, 89, 90, 93, 95, 96, 98, 99}\n", + "{89, 34, 44, 14, 50, 25, 58, 93, 31}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(set1)\n", + "print(set2)\n", + "\n", + "set4 = set2 - set1\n", + "\n", + "print(set4)\n" ] }, { @@ -141,11 +308,73 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{0,\n", + " 5,\n", + " 6,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 13,\n", + " 19,\n", + " 20,\n", + " 21,\n", + " 22,\n", + " 23,\n", + " 26,\n", + " 27,\n", + " 32,\n", + " 35,\n", + " 36,\n", + " 37,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 43,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 49,\n", + " 53,\n", + " 54,\n", + " 59,\n", + " 61,\n", + " 63,\n", + " 64,\n", + " 69,\n", + " 71,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 79,\n", + " 80,\n", + " 83,\n", + " 87,\n", + " 88,\n", + " 90,\n", + " 95,\n", + " 96,\n", + " 98,\n", + " 99}" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "\n", + "set5 = set1 & set2\n", + "\n", + "set5\n" ] }, { @@ -157,11 +386,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "set6 = {}\n" ] }, { @@ -173,11 +404,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 32, 35, 36, 37, 39, 40, 41, 42, 43, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 57, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 76, 77, 79, 80, 81, 83, 84, 85, 86, 87, 88, 90, 92, 94, 95, 96, 97, 98, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "# set6.update(set3)\n", + "# set6.update(set5) --> Getting Typeerror cause of 0 value from set3 and set5\n", + "# set6\n", + "\n", + "# for element in set3:\n", + "# if element != 0:\n", + "# set6.update(set3)\n", + " # --> Same issue\n", + "# for element in set5:\n", + "# if element != 0:\n", + "# set6.update(set5)\n", + "\n", + "set6 = set3.union(set5)\n", + "\n", + "print(set6)" ] }, { @@ -189,11 +444,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "they are equal\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "if set1 == set6:\n", + " print(\"they are equal\")\n", + "else:\n", + " print(\"they are not equal\")" ] }, { @@ -205,11 +472,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 48, + "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 +509,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Aggregated set3, set4, and set5: {0, 1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 76, 77, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99}\n", + "Aggregated set1 and set2: {0, 1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 76, 77, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99}\n", + "The aggregated sets are equal\n" + ] + } + ], + "source": [ + "# Your code here\n", + "bigset1 = set3.union(set4, set5)\n", + "print(\"Aggregated set3, set4, and set5:\", bigset1)\n", + "\n", + "\n", + "bigset2 = set1.union(set2)\n", + "print(\"Aggregated set1 and set2:\", bigset2)\n", + "\n", + "if bigset1 == bigset2:\n", + " print(\"The aggregated sets are equal\")\n", + "else:\n", + " print(\"The aggregated sets are not equal\")" ] }, { @@ -239,11 +546,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "removed_element = set1.pop()" ] }, { @@ -259,11 +567,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Remaining elements in set1: {3, 4, 5, 6, 8, 10, 12, 13, 15, 17, 18, 20, 22, 23, 24, 26, 27, 28, 30, 32, 35, 36, 37, 40, 42, 43, 45, 46, 47, 48, 52, 53, 54, 55, 57, 60, 62, 63, 64, 65, 67, 68, 70, 72, 73, 74, 76, 77, 80, 83, 84, 85, 86, 87, 88, 90, 92, 94, 95, 96, 97, 98}\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", + "set1.difference_update(list_to_remove)\n", + "\n", + "print(\"Remaining elements in set1:\", set1)\n" ] }, { @@ -278,9 +600,18 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Grocery list with unique elements:\n", + "[['garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions', 'gums', 'flour', 'cucumber', 'watermelon', 'yogurt', 'garlic'], [], ['blueberries', 'sugar', 'watermelon', 'gums', 'tomatoes', 'yogurt', 'juice', 'milk', 'onions', 'garlic', 'cucumber', 'sugar', 'blueberries', 'gums', 'yogurt'], [], ['tomatoes', 'water', 'onions', 'blueberries', 'garlic', 'flour', 'cherries', 'tomatoes', 'onions', 'water', 'tomatoes', 'toilet paper']]\n" + ] + } + ], "source": [ "father_list = ['garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions', 'gums', 'flour', 'cucumber', 'watermelon', 'yogurt', 'garlic']\n", "\n", @@ -292,7 +623,23 @@ "\n", "Mattia_list = []\n", "\n", - "\n" + "grocery_list = []\n", + "\n", + "grocery_list.append(father_list)\n", + "grocery_list.append(mother_list)\n", + "grocery_list.append(Jo_list)\n", + "grocery_list.append(Mattia_list)\n", + "grocery_list.append(Carlos_list)\n", + "\n", + "unique_grocery_list = []\n", + "\n", + "for sublist in grocery_list:\n", + " for element in sublist:\n", + " if element not in unique_grocery_list:\n", + " unique_grocery_list.append(element)\n", + "\n", + "print(\"Grocery list with unique elements:\")\n", + "print(grocery_list)" ] }, { @@ -308,7 +655,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "#But originally mother and Mattia were empty, I don't know to whom the repeated elements belong" ] }, { @@ -322,11 +670,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'cherries',\n", + " 'cucumber',\n", + " 'flour',\n", + " 'gums',\n", + " 'juice',\n", + " 'milk',\n", + " 'sugar',\n", + " 'toilet paper',\n", + " 'water',\n", + " 'watermelon',\n", + " 'yogurt'}" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "mattia_list_items = set(Carlos_list).symmetric_difference(set(Jo_list))\n", + "mattia_list_items" ] }, { @@ -338,25 +709,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['toilet paper', 'mushrooms', 'tomatoes', 'vinegar', 'shampoo', 'tomatoes', 'bread', 'oranges', 'flour', 'bread', 'gums', 'flour', 'onions', 'bread', 'cucumber', 'juice', 'gums', 'oranges', 'oranges', 'tomatoes', 'onions', 'watermelon', 'cucumber', 'oranges', 'tomatoes', 'sugar', 'mushrooms', 'toilet paper', 'bread', 'gums', 'deodorant', 'chocolate', 'gums', 'vinegar', 'blueberries', 'cucumber', 'gums', 'gums', 'water', 'vinegar', 'juice', 'oranges', 'water', 'onions', 'garlic', 'onions', 'yogurt', 'vinegar', 'onions', 'sugar', 'milk', 'onions', 'sugar', 'mushrooms', 'onions', 'onions', 'water', 'blueberries', 'yogurt', 'garlic', 'cucumber', 'cucumber', 'onions', 'yogurt', 'chocolate', 'deodorant', 'milk', 'blueberries', 'toilet paper', 'garlic', 'bread', 'garlic', 'cherries', 'chocolate', 'cherries', 'garlic', 'bread', 'watermelon', 'juice', 'vinegar', 'blueberries', 'vinegar', 'milk', 'water', 'oranges', 'cucumber', 'blueberries', 'onions', 'oranges', 'cherries', 'gums', 'yogurt', 'oranges', 'flour', 'juice', 'sugar', 'cucumber', 'flour', 'bread', 'mushrooms']\n", + "{'deodorant', 'shampoo', 'bread', 'sugar', 'cherries', 'oranges', 'garlic', 'toilet paper', 'flour', 'tomatoes', 'blueberries', 'mushrooms', 'cucumber', 'chocolate', 'water', 'yogurt', 'watermelon', 'gums', 'juice', 'milk', 'onions', 'vinegar'}\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", "\n", - "mother_list = random.choices(items,k = 10) # https://docs.python.org/3/library/random.html\n", + "mother_list = random.choices(items, k=100) # https://docs.python.org/3/library/random.html\n", "\n", - "print(mother_list)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "mother_set = set(mother_list)\n", + "\n", + "print(mother_list)\n", + "print(mother_set)" ] }, { @@ -373,11 +747,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "mother_list = [item for item in mother_list if item != \"toilet paper\"]\n", + "\n", + "mother_set.discard(\"toilet paper\")" ] }, { @@ -389,11 +766,48 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'blueberries',\n", + " 'bread',\n", + " 'cherries',\n", + " 'chocolate',\n", + " 'cucumber',\n", + " 'deodorant',\n", + " 'flour',\n", + " 'garlic',\n", + " 'gums',\n", + " 'juice',\n", + " 'milk',\n", + " 'mushrooms',\n", + " 'onions',\n", + " 'oranges',\n", + " 'shampoo',\n", + " 'sugar',\n", + " 'tomatoes',\n", + " 'vinegar',\n", + " 'water',\n", + " 'watermelon',\n", + " 'yogurt'}" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "#Wouldn't that already be mother_set?\n", + "\n", + "# print(mother_set) --> nope, there are some missing\n", + "\n", + "mother_set.update(mother_list)\n", + "mother_set" ] }, { @@ -407,11 +821,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 92, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'gums', 'juice', 'milk', 'onions', 'vinegar'}" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "\n", + "for i in range(len(mother_set) - 5):\n", + " mother_set.pop()\n", + "\n", + "mother_set\n" ] } ], @@ -431,7 +861,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 7ab8ea5..87c94f6 100644 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -49,11 +49,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "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", + "word_freq = {'love': 25, 'conversation': 1, 'every': 6, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'now': 11,\n", + " 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'push': 3, 'baby': 14, 'going': 1, 'you': 16, \"don't\": 2,\n", + " 'one': 1, 'mind': 2, 'backseat': 1, 'friends': 1, 'then': 3, 'know': 2, 'take': 1,\n", + " 'play': 1, 'okay': 1, 'so': 2, 'begin': 1, 'start': 2, 'over': 1, 'body': 17, 'boy': 2,\n", + " 'just': 1, 'we': 7, 'are': 1, 'girl': 2, 'tell': 1, 'singing': 2, 'drinking': 1, 'put': 3,\n", + " 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1,\n", + " 'give': 1, \"i'm\": 23, 'like': 10, 'can': 1, 'doing': 2, 'with': 22, 'club': 1, 'come': 37, 'it': 1,\n", + " 'somebody': 2, 'handmade': 2, 'out': 1, 'new': 6, 'room': 3, 'chance': 1, 'follow': 6, 'in': 27, 'may': 2,\n", + " 'brand': 6, 'that': 2, 'magnet': 3, 'up': 3, 'first': 1, 'and': 23, 'pull': 3, 'of': 6, 'table': 1, 'much': 2,\n", + " 'last': 3, 'i': 6, 'thrifty': 1, 'grab': 2, 'was': 2, 'driver': 1, 'slow': 1, 'dance': 1, 'the': 18, 'say': 2,\n", + " 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'me': 10, 'do': 3, 'waist': 2, 'smell': 3, 'day': 6,\n", + " 'although': 3, 'your': 21, 'leave': 1, 'want': 2, \"let's\": 2, 'lead': 6, 'at': 1, 'hand': 1, 'how': 1,\n", + " 'talk': 4, 'not': 2, 'eat': 1, 'falling': 3, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'crazy': 2,\n", + " 'let': 1, 'too': 5, 'van': 1, 'shots': 1, 'go': 2, 'to': 2, 'a': 8, 'my': 33, 'is': 5, 'place': 1, 'find': 1,\n", + " 'shape': 6, 'on': 40, 'kiss': 1, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'discovering': 6, 'something': 6,\n", + " 'be': 16, 'bedsheets': 3, 'fill': 2, 'hours': 2, 'stop': 1, 'bar': 1}\n", + "\n", + "\n", + "keys = list(word_freq.keys())\n", + "\n", + "keys.sort()\n", + "\n", + "word_freq2 = {}\n", + "\n", + "for key in keys:\n", + " word_freq2[key] = word_freq[key]\n", + "\n", + "print(word_freq2)\n", + "\n", + "\n", + "\n", + "\n", + "\n" ] }, { @@ -90,11 +131,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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", + "import operator\n", + "\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "\n", + "word_freq2 = {}\n", + "for item in sorted_tups:\n", + " word_freq2[item[0]] = item[1]\n", + "\n", + "print(word_freq2)" ] }, { @@ -110,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -132,11 +190,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "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", + "df = pd.DataFrame(list(word_freq.items()), columns=['word', 'freq'])\n", + "\n", + "print(df)" ] }, { @@ -150,11 +232,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "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", + "# I guess it means alphabetically ascending, not by number of letters for colum 'word'\n", + "df = df.sort_values('word', ascending=True)\n", + "print(df)" ] }, { @@ -166,11 +272,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "126 on 40\n", + "56 come 37\n", + "121 my 33\n", + "65 in 27\n", + "0 love 25\n", + ".. ... ...\n", + "103 hand 1\n", + "48 radio 1\n", + "14 going 1\n", + "49 give 1\n", + "52 can 1\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df = df.sort_values('freq', ascending=False)\n", + "print(df)" ] } ], @@ -190,7 +319,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.9" } }, "nbformat": 4,