diff --git a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb index b65ffd1..7187638 100644 --- a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -15,12 +15,12 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Your code here\n", - "tup = (\"I\",)" + "tup = ('I',)" ] }, { @@ -34,23 +34,20 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 10, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "tuple" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] } ], "source": [ "# Your code here\n", - "type(tup)" + "print(type(tup))" ] }, { @@ -68,26 +65,14 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "ename": "AttributeError", - "evalue": "'tuple' object has no attribute 'append'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Your code here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtup\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"r\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;31m# Your explanation here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# tuple is immutable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" - ] - } - ], + "outputs": [], "source": [ "# Your code here\n", - "tup.append(\"r\")\n", + "tup.append()\n", "# Your explanation here\n", - "# tuple is immutable" + "#we cannot change the content of a tuple so, because of it, the atribute doesn't exist." ] }, { @@ -105,14 +90,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# Your code here\n", - "tup[0] = \"r\"\n", + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", "# Your explanation here\n", - "# tuple is immutable" + "#What is happening here is that we are giving the var \"tup\" an all new value. Like we would with any other variable." ] }, { @@ -130,13 +115,27 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 33, "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 = (\"I\", \"r\", \"o\", \"n\")\n", - "tup2 = (\"h\", \"a\", \"c\", \"k\")" + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + " \n", + "tup1 = tup[:4]\n", + "print(tup1)\n", + "tup2 = tup[-4:]\n", + "print(tup2)\n", + "\n" ] }, { @@ -150,26 +149,25 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", - "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", - "True\n" + "Tup3: ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "Tup : ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" ] } ], "source": [ "# Your code here\n", - "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", - "tup3 = tup1 + tup2\n", - "print(tup3)\n", - "print(tup)\n", - "print(tup == tup3)" + "tup3 = tup1+tup2\n", + "\n", + "if (tup3 == tup):\n", + " print(\"Tup3:\",tup3)\n", + " print(\"Tup :\",tup)" ] }, { @@ -181,26 +179,28 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "4\n", - "8\n", - "8\n", - "8\n" + "The sum of 'tup1'and'tup2'is the same as'tup3'.\n" ] } ], "source": [ "# Your code here\n", - "print(len(tup1))\n", - "print(len(tup3))\n", - "print(len(tup1) + len(tup2))\n", - "print(len(tup3))" + "total_tup3 = len(tup3)\n", + "total_el1 = len(tup1)\n", + "total_el2 = len(tup2)\n", + "\n", + "total_sum = total_el1 + total_el2\n", + "\n", + "if total_tup3 == total_sum:\n", + " print(\"The sum of 'tup1'and'tup2'is the same as'tup3'.\")\n", + " " ] }, { @@ -212,24 +212,20 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 59, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "h\n" + ] } ], "source": [ "# Your code here\n", - "tup3.index(\"h\")\n", - "##It's number 4" + "print(tup3[4])" ] }, { @@ -249,33 +245,34 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[True, False, True, False, False]\n" + "a : True\n", + "b : False\n", + "c : True\n", + "d : False\n", + "e : False\n" ] } ], "source": [ - "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", - "\n", "# Your code here\n", - "# with a for loop\n", - "#for element in letters:\n", - " # if element in tup3:\n", - " # print(True)\n", - " # else:\n", - " # print(False)\n", - "#using a list comprehension\n", "\n", - "existing_letters = [True if (element in tup3) else False for element in letters ]\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "#Tup3: ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", "\n", - "#if element in tup3 print(True) else print(False) \n", - "print(existing_letters)\n" + "for l in letters:\n", + " if l in tup3:\n", + " print(l,\":\",True)\n", + " else:\n", + " print(l,\":\",False)\n", + " \n", + " " ] }, { @@ -289,18 +286,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a appears 1 time.\n", + "b appears 0 time.\n", + "c appears 1 time.\n", + "d appears 0 time.\n", + "e appears 0 time.\n" + ] + } + ], "source": [ "# Your code here\n", - "\n" + "for l in letters:\n", + " cont=0\n", + " if l in tup3:\n", + " cont=cont+1\n", + " print(l,\"appears\",cont,\"time.\")\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -314,7 +334,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb index e46f419..4081e3b 100644 --- a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 310, "metadata": {}, "outputs": [], "source": [ @@ -24,7 +24,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### In the cell below, create a list named `sample_list_1` with 80 random values. \n", + "#### 1 - In the cell below, create a list named `sample_list_1` with 80 random values. \n", "\n", "Requirements:\n", "\n", @@ -38,34 +38,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 311, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[60, 37, 93, 59, 39, 9, 81, 50, 16, 48, 56, 65, 57, 71, 42, 86, 63, 44, 12, 82, 89, 10, 76, 87, 61, 3, 25, 31, 13, 14, 21, 51, 28, 74, 83, 1, 55, 35, 49, 68, 88, 54, 32, 97, 58, 92, 29, 2, 84, 43, 66, 18, 53, 94, 11, 17, 85, 45, 22, 67, 30, 69, 90, 77, 52, 80, 95, 98, 38, 62, 79, 4, 15, 64, 47, 24, 27, 8, 73, 99]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "import random\n", + "sample_list_1 = random.sample(range(1, 100), 80)\n", + "print(sample_list_1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Convert `sample_list_1` to a set called `set1`. Print the length of the set. Is its length still 80?" + "#### 2 - Convert `sample_list_1` to a set called `set1`. Print the length of the set. Is its length still 80?" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 312, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "{1, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 22, 24, 25, 27, 28, 29, 30, 31, 32, 35, 37, 38, 39, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 73, 74, 76, 77, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set1 = set(sample_list_1)\n", + "print(len(set1))\n", + "print(set1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Create another list named `sample_list_2` with 80 random values.\n", + "#### 3 - Create another list named `sample_list_2` with 80 random values.\n", "\n", "Requirements:\n", "\n", @@ -77,208 +100,578 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 313, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[23, 43, 41, 98, 18, 73, 35, 73, 92, 57, 44, 22, 53, 7, 80, 10, 63, 49, 67, 27, 81, 42, 26, 50, 78, 44, 83, 30, 83, 29, 53, 23, 37, 36, 78, 87, 79, 73, 84, 56, 85, 21, 22, 92, 61, 78, 12, 63, 91, 69, 23, 71, 75, 32, 32, 72, 73, 14, 1, 98, 68, 12, 89, 28, 87, 56, 64, 39, 59, 80, 72, 3, 54, 58, 40, 90, 77, 25, 45, 10]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "import random\n", + "\n", + "sample_list_2 = []\n", + "\n", + "for i in range(80):\n", + " r = random.randint(0,100)\n", + " sample_list_2.append(r)\n", + " \n", + "print(sample_list_2)" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your code here\n" + "#### 4 - Convert `sample_list_2` to a set called `set2`. Print the length of the set. Is its length still 80?" + ] + }, + { + "cell_type": "code", + "execution_count": 314, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[23, 43, 41, 98, 18, 73, 35, 73, 92, 57, 44, 22, 53, 7, 80, 10, 63, 49, 67, 27, 81, 42, 26, 50, 78, 44, 83, 30, 83, 29, 53, 23, 37, 36, 78, 87, 79, 73, 84, 56, 85, 21, 22, 92, 61, 78, 12, 63, 91, 69, 23, 71, 75, 32, 32, 72, 73, 14, 1, 98, 68, 12, 89, 28, 87, 56, 64, 39, 59, 80, 72, 3, 54, 58, 40, 90, 77, 25, 45, 10]\n", + "Length of Sample_list_2: 80\n", + "{1, 3, 7, 10, 12, 14, 18, 21, 22, 23, 25, 26, 27, 28, 29, 30, 32, 35, 36, 37, 39, 40, 41, 42, 43, 44, 45, 49, 50, 53, 54, 56, 57, 58, 59, 61, 63, 64, 67, 68, 69, 71, 72, 73, 75, 77, 78, 79, 80, 81, 83, 84, 85, 87, 89, 90, 91, 92, 98}\n", + "Length of Set2: 59\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set2 = set(sample_list_2)\n", + "print(sample_list_2)\n", + "print(\"Length of Sample_list_2:\",len(sample_list_2))\n", + "print(set2)\n", + "print(\"Length of Set2:\",len(set2))\n", + "#Sets don't allow repeated content and organize the content from < to >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Convert `sample_list_2` to a set called `set2`. Print the length of the set. Is its length still 80?" + "#### 5 - Identify the elements present in `set1` but not in `set2`. Assign the elements to a new set named `set3`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 315, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 4, 8, 9, 11, 13, 15, 16, 17, 24, 31, 38, 47, 48, 51, 52, 55, 60, 62, 65, 66, 74, 76, 82, 86, 88, 93, 94, 95, 97, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set3 = set1-set2\n", + "\n", + "print(set3)\n" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "6 - Identify the elements present in set2 but not in set1. Assign the elements to a new set named set4" + ] + }, + { + "cell_type": "code", + "execution_count": 316, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{36, 7, 40, 41, 72, 75, 78, 23, 26, 91}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set4 = set2-set1\n", + "\n", + "print(set4)" + ] + }, + { + "cell_type": "raw", "metadata": {}, - "outputs": [], "source": [ - "# Your code here\n" + "7 -Now Identify the elements shared between set1 and set2. Assign the elements to a new set named set5" + ] + }, + { + "cell_type": "code", + "execution_count": 317, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 3, 10, 12, 14, 18, 21, 22, 25, 27, 28, 29, 30, 32, 35, 37, 39, 42, 43, 44, 45, 49, 50, 53, 54, 56, 57, 58, 59, 61, 63, 64, 67, 68, 69, 71, 73, 77, 79, 80, 81, 83, 84, 85, 87, 89, 90, 92, 98}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set5 = set()\n", + "#for el1 in set1:\n", + "# for el2 in set2:\n", + "# if el1 == el2:\n", + "# set5.add(el1)\n", + "#print(set5)\n", + "set5 = set1.intersection(set2)\n", + "print(set5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Identify the elements present in `set1` but not in `set2`. Assign the elements to a new set named `set3`." + "#### 8 - Create an empty set called `set6`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 318, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set6 = set()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Identify the elements present in `set2` but not in `set1`. Assign the elements to a new set named `set4`." + "#### 9 - Add `set3` and `set5` to `set6` using the Python Set `update` method." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 319, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 22, 24, 25, 27, 28, 29, 30, 31, 32, 35, 37, 38, 39, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 73, 74, 76, 77, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set6.update(set3)\n", + "set6.update(set5)\n", + "\n", + "print(set6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Now Identify the elements shared between `set1` and `set2`. Assign the elements to a new set named `set5`." + "#### 10 - Check if `set1` and `set6` are equal." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 320, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "They are.\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "if set1==set6:\n", + " print(\"They are.\")\n", + " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### What is the relationship among the following values:\n", + "#### 11 - Check if `set1` contains `set2` using the Python Set `issubset` method. Then check if `set1` contains `set3`.*" + ] + }, + { + "cell_type": "code", + "execution_count": 321, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Set1 contains Set2 ? False\n", + "Set1 contains Set3 ? True\n" + ] + } + ], + "source": [ + "# Your code here\n", "\n", - "* len(set1)\n", - "* len(set2)\n", - "* len(set3)\n", - "* len(set4)\n", - "* len(set5)\n", + "print(\"Set1 contains Set2 ?\",set2.issubset(set1))\n", + "print(\"Set1 contains Set3 ?\",set3.issubset(set1))\n", "\n", - "Use a math formular to represent that relationship. Test your formular with Python code." + "#print(set1)\n", + "#print(set3)" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your code here\n" + "#### 12 -Using the Python Set `union` method, aggregate `set3`, `set4`, and `set5`. Then aggregate `set1` and `set2`. \n", + "\n", + "#### Check if the aggregated values are equal." + ] + }, + { + "cell_type": "code", + "execution_count": 322, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Set3 and Set4 and Set5: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 87, 88, 89, 90, 91, 92, 93, 95, 96, 97, 98, 99, 100}\n", + " \n", + "Set2 and Set3: {1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "#print(set3)\n", + "#print(set4)\n", + "#print(set5)\n", + "new_set345 = set3.union(set4)\n", + "#print(new_set)\n", + "new_set345 = new_set.union(set5)\n", + "print(\"Set3 and Set4 and Set5: \",new_set345)\n", + "new_set12 = set1.union(set2)\n", + "print(\" \")\n", + "print(\"Set2 and Set3: \",new_set12)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Create an empty set called `set6`." + "#### 13 -Using the `pop` method, remove the first element from `set1`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 323, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 22, 24, 25, 27, 28, 29, 30, 31, 32, 35, 37, 38, 39, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 73, 74, 76, 77, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "#print(set1)\n", + "set1.pop()\n", + "print(set1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Add `set3` and `set5` to `set6` using the Python Set `update` method." + "#### 14 -Remove every element in the following list from `set1` if they are present in the set. Print the remaining elements.\n", + "\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", + "```" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 369, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 4, 8, 10, 12, 13, 14, 15, 16, 17, 18, 22, 24, 25, 27, 28, 30, 32, 35, 37, 38, 42, 43, 44, 45, 47, 48, 50, 52, 53, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 67, 68, 73, 74, 76, 77, 80, 82, 83, 84, 85, 86, 87, 88, 90, 92, 93, 94, 95, 97, 98}\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", + "set_to_remove = set(list_to_remove)\n", + "\n", + "new_list = set1 - set_to_remove\n", + "print(new_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Check if `set1` and `set6` are equal." + "#### 15 -The objective of this lab is to provide you a overview of manipulating sets in Python and how they can be integrated within a real usecase\n", + "#### For the exercises below it's strongly advised to quickly check this link: https://docs.python.org/2/library/sets.html#sets.Set\n", + "#### Imagine you need to create the grocery list for your family! Below you will see a list of items each family member wants. There will be repetitions because this family has the habbit of each time they notice something is missing to write it down. The problem is that they don't really talk to each other and therefore some items in the list are repeated. With the collection of questions below you will see how they solve repetion and create a grocery list with unique elements.\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 370, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "father_list = ['garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions', 'gums', 'flour', 'cucumber','watermelon', 'yogurt', 'garlic']\n", + "mother_list = []\n", + "\n", + "Jo_list = ['blueberries', 'sugar', 'watermelon', 'gums', 'tomatoes', 'yogurt','juice', 'milk', 'onions', 'garlic', 'cucumber', 'sugar', 'blueberries', 'gums', 'yogurt']\n", + "\n", + "Carlos_list = ['tomatoes', 'water', 'onions', 'blueberries', 'garlic', 'flour', 'cherries', 'tomatoes','onions', 'water', 'tomatoes', 'toilet paper']\n", + "Mattia_list = []\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Check if `set1` contains `set2` using the Python Set `issubset` method. Then check if `set1` contains `set3`.*" + "#### 15.1- As you can see there are items repeated in each list. Creat a sequence of iterable elements with dintinct items that each family member wants. You can do it in two different ways. Assign each one (set) to a variable with the name of the family member (ex: father_set)." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 371, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "father_set = set(father_list)\n", + "mother_set = set(mother_list)\n", + "Jo_set = set(Jo_list)\n", + "Carlos_set = set(Carlos_list)\n", + "Mattia_set = set(Mattia_list)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Using the Python Set `union` method, aggregate `set3`, `set4`, and `set5`. Then aggregate `set1` and `set2`. \n", + "#### 15.2- Q: Mattia wants the same items that his brother Carlos and his sister Jo but doesn't want anything they both want.\n", "\n", - "#### Check if the aggregated values are equal." + "##### Hint: check out the documentation for `x.symmetric_difference(y)`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 372, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'gums', 'yogurt', 'cucumber', 'water', 'juice', 'flour', 'milk', 'sugar', 'toilet paper', 'watermelon', 'cherries'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "Mattia_set = set(Carlos_set.symmetric_difference(Jo_set))\n", + "print(Mattia_set)\n" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your code here\n" + "#### 15.3- The line of code below will generate a list of random elements from a collection of items, with replacement. Run it and from it create a sequence of unique elements and assign it to the variable mother.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 373, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['onions', 'milk', 'oranges', 'garlic', 'water', 'gums', 'cucumber', 'tomatoes', 'tomatoes', 'watermelon']\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", + "\n", + "print(mother_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 374, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'water', 'gums', 'cucumber', 'garlic', 'milk', 'oranges', 'watermelon', 'tomatoes', 'onions'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "mother_set = set(mother_list)\n", + "print(mother_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Using the `pop` method, remove the first element from `set1`." + "#### 15.4- Before leaving the house to the grocery store the father found a lot of toilet paper, so the mother decided to remove it from the items needed to be bought. Use the `.discard()` method to remove from each set created before the 'toilet paper'. \n", + "\n", + "#### Hint: Try to first create a list contaning the variables of each set created before, then loop through them and remove the unecessary item. \n", + "\n", + "##### Note: The`.discard()` method will remove and update the set without the need of reassignment.\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 375, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Father List: {'gums', 'yogurt', 'cucumber', 'garlic', 'flour', 'watermelon', 'onions'}\n", + "Mother List: {'water', 'gums', 'cucumber', 'garlic', 'milk', 'oranges', 'watermelon', 'tomatoes', 'onions'}\n", + "Jo List : {'gums', 'yogurt', 'cucumber', 'juice', 'garlic', 'milk', 'sugar', 'blueberries', 'watermelon', 'tomatoes', 'onions'}\n", + "Carlos List: {'water', 'garlic', 'flour', 'blueberries', 'cherries', 'tomatoes', 'onions'}\n", + "Mattia List: {'gums', 'yogurt', 'cucumber', 'water', 'juice', 'flour', 'milk', 'sugar', 'watermelon', 'cherries'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "fam_list =[father_set,mother_set,Jo_set,Carlos_set,Mattia_set]\n", + "\n", + "for each_list in fam_list:\n", + " each_list.discard(\"toilet paper\")\n", + "\n", + "#Each lists without Toilet Paper:\n", + "print(\"Father List: \",father_set)\n", + "print(\"Mother List: \",mother_set)\n", + "print(\"Jo List : \",Jo_set)\n", + "print(\"Carlos List: \",Carlos_set)\n", + "print(\"Mattia List: \",Mattia_set)\n" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your code here\n" + "#### 15.5- Create a set with all the groceries needed to be bought for the house (no repetitions)." + ] + }, + { + "cell_type": "code", + "execution_count": 376, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Shopping List: {'water', 'gums', 'cucumber', 'yogurt', 'juice', 'tomatoes', 'garlic', 'flour', 'milk', 'oranges', 'sugar', 'blueberries', 'watermelon', 'cherries', 'onions'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "groceries = set(groceries.union(father_set,mother_set,Jo_set,Carlos_set,Mattia_set))\n", + "\n", + "print(\"Shopping List: \",groceries)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Remove every element in the following list from `set1` if they are present in the set. Print the remaining elements.\n", + "#### 15.6- The Father said that he could only buy five items this time. So he decided to arbitraly pick wich ones to buy this time.\n", "\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", - "```" + "##### Hint: use the `.pop()` method." + ] + }, + { + "cell_type": "code", + "execution_count": 377, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Because I'm the Boss around here, the new shopping list is this: {'sugar', 'blueberries', 'watermelon', 'cherries', 'onions'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "size = len(groceries)\n", + "\n", + "while (size > 5):\n", + " groceries.pop()\n", + " size-=1\n", + " \n", + "print(\"Because I'm the Boss around here, the new shopping list is this:\",groceries)\n" ] }, { @@ -286,14 +679,12 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here\n" - ] + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -307,7 +698,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb index d976a22..8e5f17e 100644 --- a/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 132, "metadata": {}, "outputs": [], "source": [ @@ -49,17 +49,39 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 96, "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", - "keys = word_freq.keys()\n", + "#1\n", + "keys = []\n", + "for el in word_freq:\n", + " keys.append(el)\n", + "#2\n", "keys.sort()\n", + "\n", + "#3\n", "word_freq2 = {}\n", "\n", - "for key in keys.keys():\n", - " " + "#4\n", + "#Use a FOR loop to iterate each value in keys. For each key iterated\n", + "#find the corresponding value in word_freq and insert the key-value pair to word_freq2.\n", + "\n", + "for k in keys:\n", + " for i in word_freq:\n", + " word_freq2[k] = word_freq[k]\n", + " \n", + "print(word_freq2) \n", + " " ] }, { @@ -96,11 +118,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 124, "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", + "#Using sorted and operator.itemgetter, obtain a list of tuples of the dict key-value pairs which is sorted on the value.\n", + "#Create an empty dictionary named word_freq2.\n", + "#Iterate the list of tuples. Insert each key-value pair into word_freq2 as an object.\n", + "#Print word_freq2 to confirm your dictionary has its values sorted. Your output should be:\n", + "import operator\n", + "\n", + "list_of_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "word_freq2 = {}\n", + "\n", + "for t in list_of_tups:\n", + " word_freq2 = dict((x, y) for x, y in list_of_tups)\n", + "\n", + "print(word_freq2)" ] }, { @@ -116,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 133, "metadata": {}, "outputs": [], "source": [ @@ -138,11 +182,127 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 147, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordfreq
0love25
1conversation1
2every6
3we're1
4plate1
.........
135bedsheets3
136fill2
137hours2
138stop1
139bar1
\n", + "

140 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " 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]" + ] + }, + "execution_count": 147, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "#lst = [208500, 181500, 223500, 140000, 250000, 143000, 307000, 200000, 129900, 118000]\n", + "import pandas as pd\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", + "df = pd.DataFrame(word_freq.items(), columns=['word', 'freq'])\n", + "\n", + "df\n", + "\n" ] }, { @@ -156,11 +316,120 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 154, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordfreq
120a8
109about1
43all1
96although3
72and23
.........
128were3
41where1
54with22
15you16
97your21
\n", + "

140 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " 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]" + ] + }, + "execution_count": 154, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by='word')" ] }, { @@ -172,17 +441,133 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 155, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordfreq
139bar1
114let1
116van1
60out1
57it1
.........
0love25
65in27
121my33
56come37
126on40
\n", + "

140 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " word freq\n", + "139 bar 1\n", + "114 let 1\n", + "116 van 1\n", + "60 out 1\n", + "57 it 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]" + ] + }, + "execution_count": 155, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by='freq')" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -196,7 +581,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 2e59d77..7187638 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup = ('I',)" ] }, { @@ -33,11 +34,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(type(tup))" ] }, { @@ -60,8 +70,9 @@ "outputs": [], "source": [ "# Your code here\n", - "\n", - "# Your explanation here\n" + "tup.append()\n", + "# Your explanation here\n", + "#we cannot change the content of a tuple so, because of it, the atribute doesn't exist." ] }, { @@ -79,13 +90,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# Your code here\n", - "\n", - "# Your explanation here\n" + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "# Your explanation here\n", + "#What is happening here is that we are giving the var \"tup\" an all new value. Like we would with any other variable." ] }, { @@ -103,11 +115,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "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 = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + " \n", + "tup1 = tup[:4]\n", + "print(tup1)\n", + "tup2 = tup[-4:]\n", + "print(tup2)\n", + "\n" ] }, { @@ -121,11 +149,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tup3: ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "Tup : ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup3 = tup1+tup2\n", + "\n", + "if (tup3 == tup):\n", + " print(\"Tup3:\",tup3)\n", + " print(\"Tup :\",tup)" ] }, { @@ -137,11 +179,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The sum of 'tup1'and'tup2'is the same as'tup3'.\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "total_tup3 = len(tup3)\n", + "total_el1 = len(tup1)\n", + "total_el2 = len(tup2)\n", + "\n", + "total_sum = total_el1 + total_el2\n", + "\n", + "if total_tup3 == total_sum:\n", + " print(\"The sum of 'tup1'and'tup2'is the same as'tup3'.\")\n", + " " ] }, { @@ -153,11 +212,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "h\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(tup3[4])" ] }, { @@ -177,11 +245,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a : True\n", + "b : False\n", + "c : True\n", + "d : False\n", + "e : False\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "#Tup3: ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "\n", + "for l in letters:\n", + " if l in tup3:\n", + " print(l,\":\",True)\n", + " else:\n", + " print(l,\":\",False)\n", + " \n", + " " ] }, { @@ -195,17 +286,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a appears 1 time.\n", + "b appears 0 time.\n", + "c appears 1 time.\n", + "d appears 0 time.\n", + "e appears 0 time.\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "for l in letters:\n", + " cont=0\n", + " if l in tup3:\n", + " cont=cont+1\n", + " print(l,\"appears\",cont,\"time.\")\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -219,7 +334,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index cb3a3e0..4081e3b 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": 310, "metadata": {}, "outputs": [], "source": [ @@ -38,11 +38,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 311, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[60, 37, 93, 59, 39, 9, 81, 50, 16, 48, 56, 65, 57, 71, 42, 86, 63, 44, 12, 82, 89, 10, 76, 87, 61, 3, 25, 31, 13, 14, 21, 51, 28, 74, 83, 1, 55, 35, 49, 68, 88, 54, 32, 97, 58, 92, 29, 2, 84, 43, 66, 18, 53, 94, 11, 17, 85, 45, 22, 67, 30, 69, 90, 77, 52, 80, 95, 98, 38, 62, 79, 4, 15, 64, 47, 24, 27, 8, 73, 99]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "import random\n", + "sample_list_1 = random.sample(range(1, 100), 80)\n", + "print(sample_list_1)" ] }, { @@ -54,11 +65,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 312, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "{1, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 22, 24, 25, 27, 28, 29, 30, 31, 32, 35, 37, 38, 39, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 73, 74, 76, 77, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set1 = set(sample_list_1)\n", + "print(len(set1))\n", + "print(set1)" ] }, { @@ -77,11 +100,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 313, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[23, 43, 41, 98, 18, 73, 35, 73, 92, 57, 44, 22, 53, 7, 80, 10, 63, 49, 67, 27, 81, 42, 26, 50, 78, 44, 83, 30, 83, 29, 53, 23, 37, 36, 78, 87, 79, 73, 84, 56, 85, 21, 22, 92, 61, 78, 12, 63, 91, 69, 23, 71, 75, 32, 32, 72, 73, 14, 1, 98, 68, 12, 89, 28, 87, 56, 64, 39, 59, 80, 72, 3, 54, 58, 40, 90, 77, 25, 45, 10]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "import random\n", + "\n", + "sample_list_2 = []\n", + "\n", + "for i in range(80):\n", + " r = random.randint(0,100)\n", + " sample_list_2.append(r)\n", + " \n", + "print(sample_list_2)" ] }, { @@ -93,11 +133,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 314, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[23, 43, 41, 98, 18, 73, 35, 73, 92, 57, 44, 22, 53, 7, 80, 10, 63, 49, 67, 27, 81, 42, 26, 50, 78, 44, 83, 30, 83, 29, 53, 23, 37, 36, 78, 87, 79, 73, 84, 56, 85, 21, 22, 92, 61, 78, 12, 63, 91, 69, 23, 71, 75, 32, 32, 72, 73, 14, 1, 98, 68, 12, 89, 28, 87, 56, 64, 39, 59, 80, 72, 3, 54, 58, 40, 90, 77, 25, 45, 10]\n", + "Length of Sample_list_2: 80\n", + "{1, 3, 7, 10, 12, 14, 18, 21, 22, 23, 25, 26, 27, 28, 29, 30, 32, 35, 36, 37, 39, 40, 41, 42, 43, 44, 45, 49, 50, 53, 54, 56, 57, 58, 59, 61, 63, 64, 67, 68, 69, 71, 72, 73, 75, 77, 78, 79, 80, 81, 83, 84, 85, 87, 89, 90, 91, 92, 98}\n", + "Length of Set2: 59\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set2 = set(sample_list_2)\n", + "print(sample_list_2)\n", + "print(\"Length of Sample_list_2:\",len(sample_list_2))\n", + "print(set2)\n", + "print(\"Length of Set2:\",len(set2))\n", + "#Sets don't allow repeated content and organize the content from < to >" ] }, { @@ -109,43 +166,81 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 315, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 4, 8, 9, 11, 13, 15, 16, 17, 24, 31, 38, 47, 48, 51, 52, 55, 60, 62, 65, 66, 74, 76, 82, 86, 88, 93, 94, 95, 97, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set3 = set1-set2\n", + "\n", + "print(set3)\n" ] }, { - "cell_type": "markdown", + "cell_type": "raw", "metadata": {}, "source": [ - "#### 6 - Identify the elements present in `set2` but not in `set1`. Assign the elements to a new set named `set4`." + "6 - Identify the elements present in set2 but not in set1. Assign the elements to a new set named set4" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 316, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{36, 7, 40, 41, 72, 75, 78, 23, 26, 91}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set4 = set2-set1\n", + "\n", + "print(set4)" ] }, { - "cell_type": "markdown", + "cell_type": "raw", "metadata": {}, "source": [ - "#### 7 - Now Identify the elements shared between `set1` and `set2`. Assign the elements to a new set named `set5`." + "7 -Now Identify the elements shared between set1 and set2. Assign the elements to a new set named set5" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 317, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 3, 10, 12, 14, 18, 21, 22, 25, 27, 28, 29, 30, 32, 35, 37, 39, 42, 43, 44, 45, 49, 50, 53, 54, 56, 57, 58, 59, 61, 63, 64, 67, 68, 69, 71, 73, 77, 79, 80, 81, 83, 84, 85, 87, 89, 90, 92, 98}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set5 = set()\n", + "#for el1 in set1:\n", + "# for el2 in set2:\n", + "# if el1 == el2:\n", + "# set5.add(el1)\n", + "#print(set5)\n", + "set5 = set1.intersection(set2)\n", + "print(set5)" ] }, { @@ -157,11 +252,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 318, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set6 = set()" ] }, { @@ -173,11 +269,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 319, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 22, 24, 25, 27, 28, 29, 30, 31, 32, 35, 37, 38, 39, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 73, 74, 76, 77, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set6.update(set3)\n", + "set6.update(set5)\n", + "\n", + "print(set6)" ] }, { @@ -189,11 +297,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 320, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "They are.\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "if set1==set6:\n", + " print(\"They are.\")\n", + " " ] }, { @@ -205,11 +324,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 321, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Set1 contains Set2 ? False\n", + "Set1 contains Set3 ? True\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "print(\"Set1 contains Set2 ?\",set2.issubset(set1))\n", + "print(\"Set1 contains Set3 ?\",set3.issubset(set1))\n", + "\n", + "#print(set1)\n", + "#print(set3)" ] }, { @@ -223,11 +357,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 322, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Set3 and Set4 and Set5: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 87, 88, 89, 90, 91, 92, 93, 95, 96, 97, 98, 99, 100}\n", + " \n", + "Set2 and Set3: {1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "#print(set3)\n", + "#print(set4)\n", + "#print(set5)\n", + "new_set345 = set3.union(set4)\n", + "#print(new_set)\n", + "new_set345 = new_set.union(set5)\n", + "print(\"Set3 and Set4 and Set5: \",new_set345)\n", + "new_set12 = set1.union(set2)\n", + "print(\" \")\n", + "print(\"Set2 and Set3: \",new_set12)" ] }, { @@ -239,11 +393,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 323, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 22, 24, 25, 27, 28, 29, 30, 31, 32, 35, 37, 38, 39, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 73, 74, 76, 77, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "#print(set1)\n", + "set1.pop()\n", + "print(set1)" ] }, { @@ -259,11 +424,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 369, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 4, 8, 10, 12, 13, 14, 15, 16, 17, 18, 22, 24, 25, 27, 28, 30, 32, 35, 37, 38, 42, 43, 44, 45, 47, 48, 50, 52, 53, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 67, 68, 73, 74, 76, 77, 80, 82, 83, 84, 85, 86, 87, 88, 90, 92, 93, 94, 95, 97, 98}\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", + "set_to_remove = set(list_to_remove)\n", + "\n", + "new_list = set1 - set_to_remove\n", + "print(new_list)" ] }, { @@ -278,21 +456,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 370, "metadata": {}, "outputs": [], "source": [ - "father_list = ['garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions', 'gums', 'flour', 'cucumber', 'watermelon', 'yogurt', 'garlic']\n", - "\n", + "father_list = ['garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions', 'gums', 'flour', 'cucumber','watermelon', 'yogurt', 'garlic']\n", "mother_list = []\n", "\n", - "Jo_list = ['blueberries', 'sugar', 'watermelon', 'gums', 'tomatoes', 'yogurt', 'juice', 'milk', 'onions', 'garlic', 'cucumber', 'sugar', 'blueberries', 'gums', 'yogurt']\n", - "\n", - "Carlos_list = ['tomatoes', 'water', 'onions', 'blueberries', 'garlic', 'flour', 'cherries', 'tomatoes', 'onions', 'water', 'tomatoes', 'toilet paper']\n", - "\n", - "Mattia_list = []\n", + "Jo_list = ['blueberries', 'sugar', 'watermelon', 'gums', 'tomatoes', 'yogurt','juice', 'milk', 'onions', 'garlic', 'cucumber', 'sugar', 'blueberries', 'gums', 'yogurt']\n", "\n", - "\n" + "Carlos_list = ['tomatoes', 'water', 'onions', 'blueberries', 'garlic', 'flour', 'cherries', 'tomatoes','onions', 'water', 'tomatoes', 'toilet paper']\n", + "Mattia_list = []\n" ] }, { @@ -304,11 +478,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 371, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "father_set = set(father_list)\n", + "mother_set = set(mother_list)\n", + "Jo_set = set(Jo_list)\n", + "Carlos_set = set(Carlos_list)\n", + "Mattia_set = set(Mattia_list)\n" ] }, { @@ -322,11 +502,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 372, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'gums', 'yogurt', 'cucumber', 'water', 'juice', 'flour', 'milk', 'sugar', 'toilet paper', 'watermelon', 'cherries'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "Mattia_set = set(Carlos_set.symmetric_difference(Jo_set))\n", + "print(Mattia_set)\n" ] }, { @@ -338,9 +529,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 373, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['onions', 'milk', 'oranges', 'garlic', 'water', 'gums', 'cucumber', 'tomatoes', 'tomatoes', 'watermelon']\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 +551,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 374, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'water', 'gums', 'cucumber', 'garlic', 'milk', 'oranges', 'watermelon', 'tomatoes', 'onions'}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "mother_set = set(mother_list)\n", + "print(mother_set)" ] }, { @@ -373,11 +582,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 375, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Father List: {'gums', 'yogurt', 'cucumber', 'garlic', 'flour', 'watermelon', 'onions'}\n", + "Mother List: {'water', 'gums', 'cucumber', 'garlic', 'milk', 'oranges', 'watermelon', 'tomatoes', 'onions'}\n", + "Jo List : {'gums', 'yogurt', 'cucumber', 'juice', 'garlic', 'milk', 'sugar', 'blueberries', 'watermelon', 'tomatoes', 'onions'}\n", + "Carlos List: {'water', 'garlic', 'flour', 'blueberries', 'cherries', 'tomatoes', 'onions'}\n", + "Mattia List: {'gums', 'yogurt', 'cucumber', 'water', 'juice', 'flour', 'milk', 'sugar', 'watermelon', 'cherries'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "fam_list =[father_set,mother_set,Jo_set,Carlos_set,Mattia_set]\n", + "\n", + "for each_list in fam_list:\n", + " each_list.discard(\"toilet paper\")\n", + "\n", + "#Each lists without Toilet Paper:\n", + "print(\"Father List: \",father_set)\n", + "print(\"Mother List: \",mother_set)\n", + "print(\"Jo List : \",Jo_set)\n", + "print(\"Carlos List: \",Carlos_set)\n", + "print(\"Mattia List: \",Mattia_set)\n" ] }, { @@ -389,11 +622,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 376, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Shopping List: {'water', 'gums', 'cucumber', 'yogurt', 'juice', 'tomatoes', 'garlic', 'flour', 'milk', 'oranges', 'sugar', 'blueberries', 'watermelon', 'cherries', 'onions'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "groceries = set(groceries.union(father_set,mother_set,Jo_set,Carlos_set,Mattia_set))\n", + "\n", + "print(\"Shopping List: \",groceries)" ] }, { @@ -405,19 +649,42 @@ "##### Hint: use the `.pop()` method." ] }, + { + "cell_type": "code", + "execution_count": 377, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Because I'm the Boss around here, the new shopping list is this: {'sugar', 'blueberries', 'watermelon', 'cherries', 'onions'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "size = len(groceries)\n", + "\n", + "while (size > 5):\n", + " groceries.pop()\n", + " size-=1\n", + " \n", + "print(\"Because I'm the Boss around here, the new shopping list is this:\",groceries)\n" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here\n" - ] + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -431,7 +698,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 7ab8ea5..8e5f17e 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": 132, "metadata": {}, "outputs": [], "source": [ @@ -49,11 +49,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 96, "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", + "#1\n", + "keys = []\n", + "for el in word_freq:\n", + " keys.append(el)\n", + "#2\n", + "keys.sort()\n", + "\n", + "#3\n", + "word_freq2 = {}\n", + "\n", + "#4\n", + "#Use a FOR loop to iterate each value in keys. For each key iterated\n", + "#find the corresponding value in word_freq and insert the key-value pair to word_freq2.\n", + "\n", + "for k in keys:\n", + " for i in word_freq:\n", + " word_freq2[k] = word_freq[k]\n", + " \n", + "print(word_freq2) \n", + " " ] }, { @@ -90,11 +118,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 124, "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", + "#Using sorted and operator.itemgetter, obtain a list of tuples of the dict key-value pairs which is sorted on the value.\n", + "#Create an empty dictionary named word_freq2.\n", + "#Iterate the list of tuples. Insert each key-value pair into word_freq2 as an object.\n", + "#Print word_freq2 to confirm your dictionary has its values sorted. Your output should be:\n", + "import operator\n", + "\n", + "list_of_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "word_freq2 = {}\n", + "\n", + "for t in list_of_tups:\n", + " word_freq2 = dict((x, y) for x, y in list_of_tups)\n", + "\n", + "print(word_freq2)" ] }, { @@ -110,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 133, "metadata": {}, "outputs": [], "source": [ @@ -132,11 +182,127 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 147, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordfreq
0love25
1conversation1
2every6
3we're1
4plate1
.........
135bedsheets3
136fill2
137hours2
138stop1
139bar1
\n", + "

140 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " 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]" + ] + }, + "execution_count": 147, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "#lst = [208500, 181500, 223500, 140000, 250000, 143000, 307000, 200000, 129900, 118000]\n", + "import pandas as pd\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", + "df = pd.DataFrame(word_freq.items(), columns=['word', 'freq'])\n", + "\n", + "df\n", + "\n" ] }, { @@ -150,11 +316,120 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 154, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordfreq
120a8
109about1
43all1
96although3
72and23
.........
128were3
41where1
54with22
15you16
97your21
\n", + "

140 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " 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]" + ] + }, + "execution_count": 154, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by='word')" ] }, { @@ -166,17 +441,133 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 155, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordfreq
139bar1
114let1
116van1
60out1
57it1
.........
0love25
65in27
121my33
56come37
126on40
\n", + "

140 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " word freq\n", + "139 bar 1\n", + "114 let 1\n", + "116 van 1\n", + "60 out 1\n", + "57 it 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]" + ] + }, + "execution_count": 155, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by='freq')" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -190,7 +581,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.13" } }, "nbformat": 4,