diff --git a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb index b65ffd1..8b9abeb 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": 2, "metadata": {}, "outputs": [], "source": [ "# Your code here\n", - "tup = (\"I\",)" + "tup = ('I',)" ] }, { @@ -34,7 +34,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -43,7 +43,7 @@ "tuple" ] }, - "execution_count": 2, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 60, "metadata": {}, "outputs": [ { @@ -78,16 +78,17 @@ "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;32m/var/folders/vh/7qkb52p54kq6xrbpd6rqjwsm0000gp/T/ipykernel_50547/2826102080.py\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[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'o'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'n'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'h'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'c'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'k'\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[0m\n", "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" ] } ], "source": [ "# Your code here\n", - "tup.append(\"r\")\n", + "tup.append('r','o','n','h','a','c','k')\n", "# Your explanation here\n", - "# tuple is immutable" + "You cannot add elements to a tuple, instead you would have to use another method to do so. \n", + "For instance, convert to a list, add the elements and convert back to a tuple. " ] }, { @@ -105,14 +106,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/vh/7qkb52p54kq6xrbpd6rqjwsm0000gp/T/ipykernel_50547/3689951132.py\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[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mtup\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mtup\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'r o n h a c k'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;31m# Your explanation here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], "source": [ "# Your code here\n", - "tup[0] = \"r\"\n", + "tup[0]\n", + "tup[0] = 'r o n h a c k'\n", "# Your explanation here\n", - "# tuple is immutable" + "Tuples are immutable, once you have assigned a value to the tuple you cannot reassign a new value " ] }, { @@ -130,13 +144,28 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 4, "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", + "\n", + "tup1 = tup[:4]\n", + "\n", + "tup2 = tup[-4:]\n", + "print(tup1)\n", + "print(tup2)\n" ] }, { @@ -150,7 +179,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -165,10 +194,11 @@ ], "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", + "\n", + "\n", "print(tup == tup3)" ] }, @@ -181,7 +211,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -189,7 +219,7 @@ "output_type": "stream", "text": [ "4\n", - "8\n", + "4\n", "8\n", "8\n" ] @@ -197,8 +227,9 @@ ], "source": [ "# Your code here\n", + "\n", "print(len(tup1))\n", - "print(len(tup3))\n", + "print(len(tup2))\n", "print(len(tup1) + len(tup2))\n", "print(len(tup3))" ] @@ -212,7 +243,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -221,15 +252,14 @@ "4" ] }, - "execution_count": 32, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Your code here\n", - "tup3.index(\"h\")\n", - "##It's number 4" + "tup3.index('h')" ] }, { @@ -249,33 +279,27 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[True, False, True, False, False]\n" + "True\n", + "False\n", + "True\n", + "False\n", + "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", + "letters = ['a', 'b', 'c', 'd', 'e']\n", "\n", - "existing_letters = [True if (element in tup3) else False for element in letters ]\n", - "\n", - "#if element in tup3 print(True) else print(False) \n", - "print(existing_letters)\n" + "for letter in letters:\n", + " print(letter in tup3)" ] }, { @@ -289,18 +313,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "0\n", + "1\n", + "0\n", + "0\n" + ] + } + ], "source": [ "# Your code here\n", - "\n" + "\n", + "for letter in letters:\n", + " print(tup3.count(letter))" ] + }, + { + "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 +359,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..3f28de4 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": 40, "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": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[12, 48, 44, 74, 68, 62, 90, 0, 77, 86, 11, 87, 17, 8, 27, 5, 13, 26, 55, 82, 58, 20, 39, 83, 81, 47, 63, 42, 24, 30, 34, 16, 79, 84, 78, 89, 70, 57, 29, 33, 95, 52, 54, 23, 80, 49, 40, 51, 59, 14, 10, 71, 2, 36, 6, 15, 4, 94, 75, 98, 69, 66, 18, 53, 1, 31, 28, 76, 22, 35, 91, 3, 73, 64, 56, 92, 67, 38, 60, 21]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "sample_list_1 = random.sample (range(0,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": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "80" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_1 = set(sample_list_1)\n", + "len(set_1)" ] }, { "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,548 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[97, 10, 9, 44, 60, 80, 71, 22, 7, 42, 14, 3, 9, 8, 59, 82, 65, 90, 6, 8, 13, 15, 9, 33, 92, 63, 68, 91, 0, 85, 61, 5, 32, 2, 42, 93, 37, 52, 15, 95, 74, 31, 6, 51, 45, 71, 52, 89, 77, 7, 61, 99, 91, 23, 96, 80, 65, 16, 50, 21, 73, 16, 78, 19, 17, 8, 78, 47, 90, 69, 60, 7, 83, 31, 80, 36, 65, 34, 77, 50]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "import random\n", + "sample_list_2 = []\n", + "for _ in range(80):\n", + " sample_list_2.append(random.randint(0,100))\n", + "print(sample_list_2)" ] }, { "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?" + "#### 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": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "56" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_2 = set(sample_list_2)\n", + "len(set_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Identify the elements present in `set1` but not in `set2`. Assign the elements to a new set named `set3`." + "#### 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": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 4, 11, 12, 18, 20, 24, 26, 27, 28, 29, 30, 35, 38, 39, 40, 48, 49, 53, 54, 55, 56, 57, 58, 62, 64, 66, 67, 70, 75, 76, 79, 81, 84, 86, 87, 94, 98}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_3 = set_1.difference(set_2)\n", + "print(set_3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### 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, + "execution_count": 46, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{32, 65, 96, 97, 99, 37, 7, 9, 45, 50, 19, 93, 85, 61}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_4 = set_2.difference(set_1)\n", + "print(set_4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### 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, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 5, 6, 8, 10, 13, 14, 15, 16, 17, 21, 22, 23, 31, 33, 34, 36, 42, 44, 47, 51, 52, 59, 60, 63, 68, 69, 71, 73, 74, 77, 78, 80, 82, 83, 89, 90, 91, 92, 95}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_5 = (set_1).intersection(set_2)\n", + "print(set_5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### What is the relationship among the following values:\n", - "\n", - "* len(set1)\n", - "* len(set2)\n", - "* len(set3)\n", - "* len(set4)\n", - "* len(set5)\n", - "\n", - "Use a math formular to represent that relationship. Test your formular with Python code." + "#### 8 - Create an empty set called `set6`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_6=set()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Create an empty set called `set6`." + "#### 9 - Add `set3` and `set5` to `set6` using the Python Set `update` method." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 33, 34, 35, 36, 38, 39, 40, 42, 44, 47, 48, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 62, 63, 64, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 89, 90, 91, 92, 94, 95, 98}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_3.update(set_5)\n", + "set_6.update(set_3)\n", + "print(set_6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Add `set3` and `set5` to `set6` using the Python Set `update` method." + "#### 10 - Check if `set1` and `set6` are equal." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(set_1 == set_6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 11 - Check if `set1` contains `set2` using the Python Set `issubset` method. Then check if `set1` contains `set3`.*" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "y = set_2.issubset(set_1)\n", + "print(y)\n", + "z = set_3.issubset(set_1)\n", + "print(z)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 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": 52, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "new_set = set_3.union(set_4,set_5)\n", + "new_set_2 = set_1.union(set_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Check if `set1` and `set6` are equal." + "#### 13 -Using the `pop` method, remove the first element from `set1`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "# Your code here\n", + "remove = set_1.pop()\n", + "\n", + "print(remove)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 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": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 4, 5, 6, 8, 10, 12, 13, 14, 15, 16, 17, 18, 20, 22, 23, 24, 26, 27, 28, 30, 33, 34, 35, 36, 38, 40, 42, 44, 47, 48, 52, 53, 54, 55, 56, 57, 58, 60, 62, 63, 64, 66, 67, 68, 70, 73, 74, 75, 76, 77, 78, 80, 82, 83, 84, 86, 87, 90, 92, 94, 95, 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_1.difference_update(list_to_remove)\n", + "print(set_1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 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": 55, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "father_list = ['garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions', 'gums', 'flour', 'cucumber', 'watermelon', 'yogurt', 'garlic']\n", + "\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", + "\n", + "\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": 56, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\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)" ] }, { "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": 57, + "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": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "carlos_set.symmetric_difference(jo_set)" + ] + }, + { + "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": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['toilet paper', 'watermelon', 'tomatoes', 'watermelon', 'deodorant', 'shampoo', 'blueberries', 'sugar', 'bread', 'bread']\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": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'blueberries',\n", + " 'bread',\n", + " 'deodorant',\n", + " 'shampoo',\n", + " 'sugar',\n", + " 'toilet paper',\n", + " 'tomatoes',\n", + " 'watermelon'}" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "mother=set(mother_list)\n", + "mother" ] }, { "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": 60, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "final_set = (father_set.union(mother_set,jo_set, carlos_set, mattia_set))\n", + "\n", + "final_set.discard('toilet paper')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 15.5- Create a set with all the groceries needed to be bought for the house (no repetitions)." + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'blueberries',\n", + " 'cherries',\n", + " 'cucumber',\n", + " 'flour',\n", + " 'garlic',\n", + " 'gums',\n", + " 'juice',\n", + " 'milk',\n", + " 'onions',\n", + " 'sugar',\n", + " 'tomatoes',\n", + " 'water',\n", + " 'watermelon',\n", + " 'yogurt'}" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "final_set" ] }, { "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": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(len(final_set))\n" ] }, { @@ -287,13 +650,20 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "#final_set.pop()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -307,7 +677,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..e7f5cd6 100644 --- a/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb @@ -51,15 +51,24 @@ "cell_type": "code", "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", - "keys = word_freq.keys()\n", - "keys.sort()\n", + "keys = list(word_freq.keys())\n", + "keys= sorted(keys)\n", "word_freq2 = {}\n", "\n", - "for key in keys.keys():\n", - " " + "for value in keys:\n", + " word_freq2[value] = word_freq.get(value)\n", + "print(word_freq2)" ] }, { @@ -96,11 +105,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", + "from operator import itemgetter\n", + "\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "#print(sorted_tups)\n", + "\n", + "word_freq2 = {}\n", + "word_freq2 = dict(sorted(word_freq.items(), key=operator.itemgetter(1)))\n", + "print(word_freq2)" ] }, { @@ -116,7 +142,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -138,11 +164,82 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "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", + "
wordfreq
0love25
1conversation1
2every6
3we're1
4plate1
\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" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df = pd.DataFrame(word_freq.items(), columns=['word', 'freq'])\n", + "df.head(5)" ] }, { @@ -156,11 +253,120 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "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": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by=['word'])" ] }, { @@ -172,17 +378,133 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "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": 8, + "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 +518,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..8b9abeb 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": 2, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup = ('I',)" ] }, { @@ -33,11 +34,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "type(tup)" ] }, { @@ -55,13 +68,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, - "outputs": [], + "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/var/folders/vh/7qkb52p54kq6xrbpd6rqjwsm0000gp/T/ipykernel_50547/2826102080.py\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[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'o'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'n'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'h'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'c'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'k'\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[0m\n", + "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" + ] + } + ], "source": [ "# Your code here\n", - "\n", - "# Your explanation here\n" + "tup.append('r','o','n','h','a','c','k')\n", + "# Your explanation here\n", + "You cannot add elements to a tuple, instead you would have to use another method to do so. \n", + "For instance, convert to a list, add the elements and convert back to a tuple. " ] }, { @@ -79,13 +106,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/vh/7qkb52p54kq6xrbpd6rqjwsm0000gp/T/ipykernel_50547/3689951132.py\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[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mtup\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mtup\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'r o n h a c k'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;31m# Your explanation here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], "source": [ "# Your code here\n", - "\n", - "# Your explanation here\n" + "tup[0]\n", + "tup[0] = 'r o n h a c k'\n", + "# Your explanation here\n", + "Tuples are immutable, once you have assigned a value to the tuple you cannot reassign a new value " ] }, { @@ -103,11 +144,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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", + "\n", + "tup1 = tup[:4]\n", + "\n", + "tup2 = tup[-4:]\n", + "print(tup1)\n", + "print(tup2)\n" ] }, { @@ -121,11 +179,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "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" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup3 = tup1 + tup2\n", + "print(tup3)\n", + "print(tup)\n", + "\n", + "\n", + "print(tup == tup3)" ] }, { @@ -137,11 +211,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "4\n", + "8\n", + "8\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "print(len(tup1))\n", + "print(len(tup2))\n", + "print(len(tup1) + len(tup2))\n", + "print(len(tup3))" ] }, { @@ -153,11 +243,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup3.index('h')" ] }, { @@ -177,11 +279,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "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", + "letters = ['a', 'b', 'c', 'd', 'e']\n", + "\n", + "for letter in letters:\n", + " print(letter in tup3)" ] }, { @@ -195,17 +313,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "0\n", + "1\n", + "0\n", + "0\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "for letter in letters:\n", + " print(tup3.count(letter))" ] + }, + { + "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 +359,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..3f28de4 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": 40, "metadata": {}, "outputs": [], "source": [ @@ -38,11 +38,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[12, 48, 44, 74, 68, 62, 90, 0, 77, 86, 11, 87, 17, 8, 27, 5, 13, 26, 55, 82, 58, 20, 39, 83, 81, 47, 63, 42, 24, 30, 34, 16, 79, 84, 78, 89, 70, 57, 29, 33, 95, 52, 54, 23, 80, 49, 40, 51, 59, 14, 10, 71, 2, 36, 6, 15, 4, 94, 75, 98, 69, 66, 18, 53, 1, 31, 28, 76, 22, 35, 91, 3, 73, 64, 56, 92, 67, 38, 60, 21]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "sample_list_1 = random.sample (range(0,100),80)\n", + "print(sample_list_1)" ] }, { @@ -54,11 +64,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "80" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_1 = set(sample_list_1)\n", + "len(set_1)" ] }, { @@ -77,11 +100,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[97, 10, 9, 44, 60, 80, 71, 22, 7, 42, 14, 3, 9, 8, 59, 82, 65, 90, 6, 8, 13, 15, 9, 33, 92, 63, 68, 91, 0, 85, 61, 5, 32, 2, 42, 93, 37, 52, 15, 95, 74, 31, 6, 51, 45, 71, 52, 89, 77, 7, 61, 99, 91, 23, 96, 80, 65, 16, 50, 21, 73, 16, 78, 19, 17, 8, 78, 47, 90, 69, 60, 7, 83, 31, 80, 36, 65, 34, 77, 50]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "import random\n", + "sample_list_2 = []\n", + "for _ in range(80):\n", + " sample_list_2.append(random.randint(0,100))\n", + "print(sample_list_2)" ] }, { @@ -93,11 +129,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "56" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_2 = set(sample_list_2)\n", + "len(set_2)" ] }, { @@ -109,11 +158,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 4, 11, 12, 18, 20, 24, 26, 27, 28, 29, 30, 35, 38, 39, 40, 48, 49, 53, 54, 55, 56, 57, 58, 62, 64, 66, 67, 70, 75, 76, 79, 81, 84, 86, 87, 94, 98}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_3 = set_1.difference(set_2)\n", + "print(set_3)" ] }, { @@ -125,11 +184,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{32, 65, 96, 97, 99, 37, 7, 9, 45, 50, 19, 93, 85, 61}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_4 = set_2.difference(set_1)\n", + "print(set_4)" ] }, { @@ -141,11 +210,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 5, 6, 8, 10, 13, 14, 15, 16, 17, 21, 22, 23, 31, 33, 34, 36, 42, 44, 47, 51, 52, 59, 60, 63, 68, 69, 71, 73, 74, 77, 78, 80, 82, 83, 89, 90, 91, 92, 95}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_5 = (set_1).intersection(set_2)\n", + "print(set_5)" ] }, { @@ -157,11 +236,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_6=set()" ] }, { @@ -173,11 +253,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 33, 34, 35, 36, 38, 39, 40, 42, 44, 47, 48, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 62, 63, 64, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 89, 90, 91, 92, 94, 95, 98}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_3.update(set_5)\n", + "set_6.update(set_3)\n", + "print(set_6)" ] }, { @@ -189,11 +280,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(set_1 == set_6)" ] }, { @@ -205,11 +305,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "y = set_2.issubset(set_1)\n", + "print(y)\n", + "z = set_3.issubset(set_1)\n", + "print(z)" ] }, { @@ -223,11 +337,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "new_set = set_3.union(set_4,set_5)\n", + "new_set_2 = set_1.union(set_2)" ] }, { @@ -239,11 +355,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "# Your code here\n", + "remove = set_1.pop()\n", + "\n", + "print(remove)" ] }, { @@ -259,11 +386,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 4, 5, 6, 8, 10, 12, 13, 14, 15, 16, 17, 18, 20, 22, 23, 24, 26, 27, 28, 30, 33, 34, 35, 36, 38, 40, 42, 44, 47, 48, 52, 53, 54, 55, 56, 57, 58, 60, 62, 63, 64, 66, 67, 68, 70, 73, 74, 75, 76, 77, 78, 80, 82, 83, 84, 86, 87, 90, 92, 94, 95, 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_1.difference_update(list_to_remove)\n", + "print(set_1)" ] }, { @@ -278,7 +416,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ @@ -304,11 +442,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\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)" ] }, { @@ -322,11 +465,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 57, + "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": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "carlos_set.symmetric_difference(jo_set)" ] }, { @@ -338,9 +503,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['toilet paper', 'watermelon', 'tomatoes', 'watermelon', 'deodorant', 'shampoo', 'blueberries', 'sugar', 'bread', 'bread']\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 +525,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'blueberries',\n", + " 'bread',\n", + " 'deodorant',\n", + " 'shampoo',\n", + " 'sugar',\n", + " 'toilet paper',\n", + " 'tomatoes',\n", + " 'watermelon'}" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "mother=set(mother_list)\n", + "mother" ] }, { @@ -373,11 +566,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "final_set = (father_set.union(mother_set,jo_set, carlos_set, mattia_set))\n", + "\n", + "final_set.discard('toilet paper')" ] }, { @@ -389,11 +585,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'blueberries',\n", + " 'cherries',\n", + " 'cucumber',\n", + " 'flour',\n", + " 'garlic',\n", + " 'gums',\n", + " 'juice',\n", + " 'milk',\n", + " 'onions',\n", + " 'sugar',\n", + " 'tomatoes',\n", + " 'water',\n", + " 'watermelon',\n", + " 'yogurt'}" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "final_set" ] }, { @@ -405,19 +626,44 @@ "##### Hint: use the `.pop()` method." ] }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(len(final_set))\n" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "#final_set.pop()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -431,7 +677,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..e7f5cd6 100644 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -49,11 +49,26 @@ }, { "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", + "keys = list(word_freq.keys())\n", + "keys= sorted(keys)\n", + "word_freq2 = {}\n", + "\n", + "for value in keys:\n", + " word_freq2[value] = word_freq.get(value)\n", + "print(word_freq2)" ] }, { @@ -90,11 +105,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", + "from operator import itemgetter\n", + "\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "#print(sorted_tups)\n", + "\n", + "word_freq2 = {}\n", + "word_freq2 = dict(sorted(word_freq.items(), key=operator.itemgetter(1)))\n", + "print(word_freq2)" ] }, { @@ -110,7 +142,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -132,11 +164,82 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "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", + "
wordfreq
0love25
1conversation1
2every6
3we're1
4plate1
\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" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df = pd.DataFrame(word_freq.items(), columns=['word', 'freq'])\n", + "df.head(5)" ] }, { @@ -150,11 +253,120 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "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": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by=['word'])" ] }, { @@ -166,17 +378,133 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "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": 8, + "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 +518,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.13" } }, "nbformat": 4,