diff --git a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb index b65ffd1..642e6b1 100644 --- a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -2,11 +2,13 @@ "cells": [ { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, "source": [ - "## Challenge 1: Tuples\n", + "Challenge 1: Tuples\n", "\n", - "#### Do you know you can create tuples with only one element?\n", + "Do you know you can create tuples with only one element?\n", "\n", "**In the cell below, define a variable `tup` with a single element `\"I\"`.**\n", "\n", @@ -15,12 +17,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n", - "tup = (\"I\",)" + "tup = (\"I\", )" ] }, { @@ -34,23 +35,20 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "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)" + "tup = (\"I\",) \n", + "print(type(tup))" ] }, { @@ -68,26 +66,26 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "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'" + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" ] } ], "source": [ - "# Your code here\n", - "tup.append(\"r\")\n", - "# Your explanation here\n", - "# tuple is immutable" + "tup = (\"I\",) \n", + "new_elements = (\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") \n", + "\n", + "new_tup = tup + new_elements\n", + "\n", + "print(new_tup)\n", + "#tuples are immutable, which means once they are created, their content cannot be changed.Adding or removing elements, which is not possible directly. Therefore, we cannot append elements to tup or any other tuple once it has been defined.\n", + "#However, if we need a tuple that includes the additional elements, we can create a new tuple that combines tup with these new elements.\n" ] }, { @@ -105,14 +103,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n", - "tup[0] = \"r\"\n", - "# Your explanation here\n", - "# tuple is immutable" + "tup = (\"I\", )\n", + "tup = (\"I\",\"r\",\"o\",\"h\",\"a\",\"c\",\"k\")\n", + "print(tup)\n", + "\n", + "# Yes we can reassign the existing top\n" ] }, { @@ -130,13 +137,26 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "tup1: ('I', 'r', 'o', 'n')\n", + "tup2: ('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", + "print(tup)\n", + "tup1 = tup[0:4] \n", + "tup2 = tup[-4:] \n", + "print(\"tup1:\", tup1)\n", + "print(\"tup2:\", tup2)\n" ] }, { @@ -150,26 +170,28 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 20, "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", + "tup3 equals to tup.\n" ] } ], "source": [ - "# Your code here\n", - "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "tup1 = (\"I\", \"r\", \"o\", \"n\")\n", + "tup2 = (\"h\", \"a\", \"c\", \"k\")\n", "tup3 = tup1 + tup2\n", - "print(tup3)\n", - "print(tup)\n", - "print(tup == tup3)" + "print(\"tup3:\", tup3)\n", + "original_tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "if tup3 == original_tup:\n", + " print(\"tup3 equals to tup.\")\n", + "else:\n", + " print(\"tup3 does not equal to tup.\")\n" ] }, { @@ -181,62 +203,61 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "4\n", - "8\n", - "8\n", - "8\n" + "The sum of elements in tup1 and tup2 is the same as the number of elements in tup3.\n" ] } ], "source": [ - "# Your code here\n", - "print(len(tup1))\n", - "print(len(tup3))\n", - "print(len(tup1) + len(tup2))\n", - "print(len(tup3))" + "tup3 = tup1 + tup2\n", + "count_tup1 = len(tup1) \n", + "count_tup2 = len(tup2)\n", + "sum_counts = count_tup1 + count_tup2\n", + "count_tup3 = len(tup3)\n", + "\n", + "if sum_counts == count_tup3:\n", + " print(\"The sum of elements in tup1 and tup2 is the same as the number of elements in tup3.\")\n", + "else:\n", + " print(\"The sum of elements in tup1 and tup2 is not the same as the number of elements in tup3.\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### What is the index number of `\"h\"` in `tup3`?" + "What is the index number of `\"h\"` in `tup3`?" ] }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 24, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "The index of 'h' in tup3 is: 4\n" + ] } ], "source": [ - "# Your code here\n", - "tup3.index(\"h\")\n", - "##It's number 4" + "tup3 = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") \n", + "index_of_h = tup3.index(\"h\")\n", + "print(\"The index of 'h' in tup3 is:\", index_of_h)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Now, use a FOR loop to check whether each letter in the following list is present in `tup3`:\n", + "Now, use a FOR loop to check whether each letter in the following list is present in `tup3`:\n", "\n", "```\n", "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", @@ -249,33 +270,30 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 25, "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": [ + "tup3 = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") \n", "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", - "\n", - "#if element in tup3 print(True) else print(False) \n", - "print(existing_letters)\n" + "for letter in letters:\n", + " if letter in tup3:\n", + " print(True)\n", + " else:\n", + " print(False)\n" ] }, { @@ -289,18 +307,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The letter 'a' appears 1 times in tup3.\n", + "The letter 'b' appears 0 times in tup3.\n", + "The letter 'c' appears 1 times in tup3.\n", + "The letter 'd' appears 0 times in tup3.\n", + "The letter 'e' appears 0 times in tup3.\n" + ] + } + ], "source": [ - "# Your code here\n", - "\n" + "tup3 = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") \n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"] \n", + "for letter in letters:\n", + " count = tup3.count(letter)\n", + " print(f\"The letter '{letter}' appears {count} times in tup3.\")\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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,9 +361,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.11.7" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb index e46f419..7db252d 100644 --- a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb @@ -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,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[57, 34, 28, 27, 95, 100, 49, 61, 25, 29, 59, 46, 85, 3, 10, 17, 56, 44, 58, 66, 74, 70, 7, 69, 5, 93, 31, 75, 99, 8, 68, 48, 41, 86, 50, 72, 22, 43, 73, 52, 24, 77, 82, 78, 15, 6, 35, 40, 39, 91, 96, 79, 87, 98, 71, 84, 1, 51, 0, 20, 11, 65, 80, 64, 42, 97, 9, 45, 62, 12, 47, 37, 81, 30, 18, 89, 38, 54, 4, 36]\n" + ] + } + ], "source": [ - "# Your code here\n" + "import random\n", + "sample_list_1 = random.sample(range(101), 80) \n", + "print(sample_list_1)\n" ] }, { "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": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The length of set1 is: 80\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = set(sample_list_1)\n", + "print(\"The length of set1 is:\", len(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,223 +96,632 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[50, 40, 84, 52, 72, 60, 55, 1, 75, 7, 4, 53, 46, 17, 97, 23, 91, 37, 100, 36, 20, 21, 27, 90, 18, 20, 44, 7, 14, 20, 43, 42, 56, 74, 9, 74, 67, 29, 36, 99, 20, 70, 13, 27, 10, 77, 62, 45, 72, 74, 87, 39, 84, 80, 13, 17, 7, 17, 3, 50, 95, 91, 18, 61, 0, 82, 30, 15, 65, 23, 58, 57, 83, 43, 20, 9, 57, 33, 54, 83]\n" + ] + } + ], "source": [ - "# Your code here\n" + "import random\n", + "sample_list_2 = []\n", + "for _ in range(80):\n", + " number = random.randint(0, 100)\n", + " sample_list_2.append(number)\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": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The length of set2 is: 57\n" + ] + } + ], "source": [ - "# Your code here\n" + "set2 = set(sample_list_2)\n", + "print(\"The length of set2 is:\", len(set2))\n" ] }, { "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": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, 6, 8, 11, 12, 22, 24, 25, 28, 31, 34, 35, 38, 41, 47, 48, 49, 51, 59, 64, 66, 68, 69, 71, 73, 78, 79, 81, 85, 86, 89, 93, 96, 98}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = set(sample_list_1)\n", + "set2 = set(sample_list_2)\n", + "set3 = set1 - set2\n", + "print(set3)" ] }, { "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": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{33, 67, 13, 14, 83, 21, 53, 23, 55, 90, 60}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = set(sample_list_1)\n", + "set2 = set(sample_list_2)\n", + "set4 = set2 - set1\n", + "print(set4)" ] }, { "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": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 3, 4, 7, 9, 10, 15, 17, 18, 20, 27, 29, 30, 36, 37, 39, 40, 42, 43, 44, 45, 46, 50, 52, 54, 56, 57, 58, 61, 62, 65, 70, 72, 74, 75, 77, 80, 82, 84, 87, 91, 95, 97, 99, 100}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = set(sample_list_1)\n", + "set2 = set(sample_list_2)\n", + "set5 = set1.intersection(set2)\n", + "print(set5)\n" ] }, { "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", + " 8 - Create an empty set called `set6`." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n", + "\n" + ] + } + ], + "source": [ + "set6 = set()\n", "\n", - "Use a math formular to represent that relationship. Test your formular with Python code." + "print(set6)\n", + "print(type(set6))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 9 - Add `set3` and `set5` to `set6` using the Python Set `update` method." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 17, 18, 20, 22, 24, 25, 27, 28, 29, 30, 31, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 56, 57, 58, 59, 61, 62, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 89, 91, 93, 95, 96, 97, 98, 99, 100}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set3 = set1 - set2\n", + "set5 = set1.intersection(set2)\n", + "set6 = set() \n", + "set6.update(set3)\n", + "set6.update(set5)\n", + "print(set6)\n", + " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Create an empty set called `set6`." + "#### 10 - Check if `set1` and `set6` are equal." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ - "# Your code here\n" + "are_equal = set1 == set6\n", + "print(are_equal)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Add `set3` and `set5` to `set6` using the Python Set `update` method." + "#### 11 - Check if `set1` contains `set2` using the Python Set `issubset` method. Then check if `set1` contains `set3`.*" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set1 contains set2: False\n", + "set1 contains set3: True\n" + ] + } + ], + "source": [ + "set1 = set(sample_list_1)\n", + "set2 = set(sample_list_2)\n", + "contains_set2 = set2.issubset(set1)\n", + "print(f\"set1 contains set2: {contains_set2}\")\n", + "contains_set3 = set3.issubset(set1)\n", + "print(f\"set1 contains set3: {contains_set3}\")\n", + "\n", + "\n", + "\n" + ] + }, + { + "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": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The aggregated values are equal: True\n" + ] + } + ], + "source": [ + "aggregate1 = set3.union(set4, set5)\n", + "aggregate2 = set1.union(set2)\n", + "are_aggregates_equal = aggregate1 == aggregate2\n", + "print(f\"The aggregated values are equal: {are_aggregates_equal}\")" ] }, { "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": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Removed element: 1\n", + "Remaining set: {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 17, 18, 20, 22, 24, 25, 27, 28, 29, 30, 31, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 56, 57, 58, 59, 61, 62, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 89, 91, 93, 95, 96, 97, 98, 99, 100}\n" + ] + } + ], + "source": [ + "removed_element = set1.pop()\n", + "\n", + "print(f\"Removed element: {removed_element}\")\n", + "print(f\"Remaining set: {set1}\")" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your code here\n" + " 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": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Remaining elements in set1: {3, 4, 5, 6, 7, 8, 10, 12, 15, 17, 18, 20, 22, 24, 25, 27, 28, 30, 34, 35, 36, 37, 38, 40, 42, 43, 44, 45, 46, 47, 48, 50, 52, 54, 56, 57, 58, 62, 64, 65, 66, 68, 70, 72, 73, 74, 75, 77, 78, 80, 82, 84, 85, 86, 87, 93, 95, 96, 97, 98, 100}\n" + ] + } + ], + "source": [ + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "for item in list_to_remove:\n", + " set1.discard(item)\n", + "print(f\"Remaining elements in set1: {set1}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Check if `set1` contains `set2` using the Python Set `issubset` method. Then check if `set1` contains `set3`.*" + "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 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": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unique Grocery List: ['cucumber', 'sugar', 'garlic', 'juice', 'flour', 'gums', 'toilet paper', 'cherries', 'onions', 'water', 'blueberries', 'tomatoes', 'watermelon', 'milk', 'yogurt']\n" + ] + } + ], + "source": [ + "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", + "\n", + "# Combine lists into one large list\n", + "combined_list = father_list + mother_list + Jo_list + Carlos_list + Mattia_list\n", + "\n", + "# Convert the combined list to a set to remove duplicates\n", + "unique_grocery_set = set(combined_list)\n", + "\n", + "unique_grocery_list = list(unique_grocery_set)\n", + "print(\"Unique Grocery List:\", unique_grocery_list)\n" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your code here\n" + "#### 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": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Father's unique items: {'cucumber', 'garlic', 'flour', 'gums', 'toilet paper', 'onions', 'watermelon', 'yogurt'}\n", + "Mother's unique items: set()\n", + "Jo's unique items: {'cucumber', 'sugar', 'garlic', 'juice', 'gums', 'onions', 'blueberries', 'tomatoes', 'watermelon', 'milk', 'yogurt'}\n", + "Carlos's unique items: {'garlic', 'flour', 'cherries', 'toilet paper', 'onions', 'water', 'tomatoes', 'blueberries'}\n", + "Mattia's unique items: set()\n" + ] + } + ], + "source": [ + "#Approach 1 - Direct Method\n", + "father_set = set(father_list)\n", + "mother_set = set() # Mother's and Mattia's lists are empty\n", + "Jo_set = set(Jo_list)\n", + "Carlos_set = set(Carlos_list)\n", + "Mattia_set = set(Mattia_list)\n", + "\n", + "print(\"Father's unique items:\", father_set)\n", + "print(\"Mother's unique items:\", mother_set)\n", + "print(\"Jo's unique items:\", Jo_set)\n", + "print(\"Carlos's unique items:\", Carlos_set)\n", + "print(\"Mattia's unique items:\", Mattia_set)\n", + "\n", + "\n", + "#Approach 2 - set comparision\n", + "father_set = {item for item in father_list}\n", + "mother_set = {item for item in mother_list}\n", + "Jo_set = {item for item in Jo_list}\n", + "Carlos_set = {item for item in Carlos_list}\n", + "Mattia_set = {item for item in Mattia_list}\n", + "print(\"Father's unique items:\", father_set)\n", + "print(\"Mother's unique items:\", mother_set)\n", + "print(\"Jo's unique items:\", Jo_set)\n", + "print(\"Carlos's unique items:\", Carlos_set)\n", + "print(\"Mattia's unique items:\", Mattia_set)\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": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mattia's unique items: {'cucumber', 'sugar', 'juice', 'flour', 'gums', 'cherries', 'toilet paper', 'water', 'watermelon', 'milk', 'yogurt'}\n" + ] + } + ], + "source": [ + "unique_to_Carlos = Carlos_set - Jo_set\n", + "\n", + "unique_to_Jo = Jo_set - Carlos_set\n", + "\n", + "Mattia_set = unique_to_Carlos | unique_to_Jo\n", + "\n", + "print(\"Mattia's unique items:\", Mattia_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": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['shampoo', 'chocolate', 'garlic', 'milk', 'water', 'shampoo', 'bread', 'yogurt', 'water', 'flour']\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": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mother (unique items): ['chocolate', 'garlic', 'flour', 'water', 'shampoo', 'bread', 'milk', 'yogurt']\n" + ] + } + ], + "source": [ + "mother = list(set(mother_list))\n", + "print(\"Mother (unique items):\", 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": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Father's items: {'onions', 'garlic', 'watermelon', 'yogurt'}\n", + "Mother's items: {'water', 'chocolate', 'milk'}\n", + "Jo's items: {'blueberries', 'sugar', 'watermelon'}\n", + "Carlos's items: {'onions', 'water', 'tomatoes', 'blueberries'}\n", + "Mattia's items: {'cherries', 'sugar'}\n" + ] + } + ], + "source": [ + "#Recreating faimly set\n", + "father_set = {'garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions'}\n", + "mother_set = {'milk', 'water', 'chocolate', 'toilet paper'} \n", + "Jo_set = {'blueberries', 'sugar', 'watermelon', 'toilet paper'}\n", + "Carlos_set = {'tomatoes', 'water', 'onions', 'blueberries', 'toilet paper'}\n", + "Mattia_set = {'sugar', 'cherries', 'toilet paper'} \n", + "family_sets = [father_set, mother_set, Jo_set, Carlos_set, Mattia_set]\n", + "\n", + "for family_set in family_sets:\n", + " family_set.discard('toilet paper')\n", + "\n", + "print(\"Father's items:\", father_set)\n", + "print(\"Mother's items:\", mother_set)\n", + "print(\"Jo's items:\", Jo_set)\n", + "print(\"Carlos's items:\", Carlos_set)\n", + "print(\"Mattia's items:\", 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": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unique Grocery List: {'chocolate', 'cucumber', 'sugar', 'garlic', 'juice', 'bread', 'flour', 'gums', 'toilet paper', 'cherries', 'onions', 'water', 'shampoo', 'blueberries', 'tomatoes', 'watermelon', 'milk', 'yogurt'}\n" + ] + } + ], + "source": [ + "combined_list = father_list + mother_list + Jo_list + Carlos_list + Mattia_list\n", + "\n", + "# Convert the combined list to a set to remove duplicates\n", + "unique_grocery_set = set(combined_list)\n", + "\n", + "print(\"Unique Grocery List:\", unique_grocery_set)\n" ] }, { "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", - "\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", - "```" + "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", + "Hint: use the `.pop()` method." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Father will buy: ['onions', 'yogurt', 'garlic', 'watermelon', 'toilet paper']\n", + "Items left in the set: {'oil', 'milk'}\n" + ] + } + ], "source": [ - "# Your code here\n" + "father_set = {'garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions','milk', 'oil'}\n", + "items_to_buy = []\n", + "for _ in range(5):\n", + " item = father_set.pop()\n", + " items_to_buy.append(item)\n", + "print(\"Father will buy:\", items_to_buy)\n", + "print(\"Items left in the set:\", father_set)" ] + }, + { + "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,9 +735,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.11.7" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb index d976a22..4cd0de8 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": null, "metadata": {}, "outputs": [], "source": [ @@ -49,24 +49,36 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "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", + "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", + "keys = list(word_freq.keys())\n", "keys.sort()\n", + "\n", "word_freq2 = {}\n", "\n", - "for key in keys.keys():\n", - " " + "for key in keys:\n", + " value = word_freq[key]\n", + " word_freq2[key] = value\n", + "\n", + "print(word_freq2)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Sort the values of `word_freq` ascendingly.\n", + " Sort the values of `word_freq` ascendingly.\n", "\n", "Sorting the values of a dictionary is more tricky than sorting the keys because a dictionary's values are not unique. Therefore you cannot use the same way you sorted dict keys to sort dict values.\n", "\n", @@ -96,11 +108,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "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", + "{('conversation', 1): 21, (\"we're\", 1): 21, ('plate', 1): 21, ('sour', 1): 21, ('jukebox', 1): 21, ('taxi', 1): 21, ('fast', 1): 21, ('bag', 1): 21, ('man', 1): 21, ('going', 1): 21, ('one', 1): 21, ('backseat', 1): 21, ('friends', 1): 21, ('take', 1): 21, ('play', 1): 21, ('okay', 1): 21, ('begin', 1): 21, ('over', 1): 21, ('just', 1): 21, ('are', 1): 21, ('tell', 1): 21, ('drinking', 1): 21, ('our', 1): 21, ('where', 1): 21, (\"i'll\", 1): 21, ('all', 1): 21, (\"isn't\", 1): 21, ('make', 1): 21, ('lover', 1): 21, ('get', 1): 21, ('radio', 1): 21, ('give', 1): 21, ('can', 1): 21, ('club', 1): 21, ('it', 1): 21, ('out', 1): 21, ('chance', 1): 21, ('first', 1): 21, ('table', 1): 21, ('thrifty', 1): 21, ('driver', 1): 21, ('slow', 1): 21, ('dance', 1): 21, ('trust', 1): 21, ('family', 1): 21, ('week', 1): 21, ('date', 1): 21, ('leave', 1): 21, ('at', 1): 21, ('hand', 1): 21, ('how', 1): 21, ('eat', 1): 21, ('about', 1): 21, ('story', 1): 21, ('sweet', 1): 21, ('best', 1): 21, ('let', 1): 21, ('van', 1): 21, ('shots', 1): 21, ('place', 1): 21, ('find', 1): 21, ('kiss', 1): 21, ('stop', 1): 21, ('bar', 1): 21, (\"don't\", 2): 21, ('mind', 2): 21, ('know', 2): 21, ('so', 2): 21, ('start', 2): 21, ('boy', 2): 21, ('girl', 2): 21, ('singing', 2): 21, ('doing', 2): 21, ('somebody', 2): 21, ('handmade', 2): 21, ('may', 2): 21, ('that', 2): 21, ('much', 2): 21, ('grab', 2): 21, ('was', 2): 21, ('say', 2): 21, ('waist', 2): 21, ('want', 2): 21, (\"let's\", 2): 21, ('not', 2): 21, ('crazy', 2): 21, ('go', 2): 21, ('to', 2): 21, ('fill', 2): 21, ('hours', 2): 21, ('push', 3): 21, ('then', 3): 21, ('put', 3): 21, ('room', 3): 21, ('magnet', 3): 21, ('up', 3): 21, ('pull', 3): 21, ('last', 3): 21, ('do', 3): 21, ('smell', 3): 21, ('although', 3): 21, ('falling', 3): 21, ('were', 3): 21, ('night', 3): 21, ('heart', 3): 21, ('for', 3): 21, ('bedsheets', 3): 21, ('talk', 4): 21, ('too', 5): 21, ('is', 5): 21, ('every', 6): 21, ('new', 6): 21, ('follow', 6): 21, ('brand', 6): 21, ('of', 6): 21, ('i', 6): 21, ('day', 6): 21, ('lead', 6): 21, ('shape', 6): 21, ('discovering', 6): 21, ('something', 6): 21, ('we', 7): 21, ('a', 8): 21, ('like', 10): 21, ('me', 10): 21, ('now', 11): 21, ('baby', 14): 21, ('you', 16): 21, ('be', 16): 21, ('body', 17): 21, ('the', 18): 21, ('your', 21): 21, ('with', 22): 21, (\"i'm\", 23): 21, ('and', 23): 21, ('love', 25): 21, ('in', 27): 21, ('my', 33): 21, ('come', 37): 21, ('on', 40): 21}\n" + ] + } + ], "source": [ - "# Your code here\n" + "import operator\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "print(sorted_tups)\n", + "word_freq2 = {}\n", + "\n", + "for key in sorted_tups:\n", + " word_freq2[key] = value\n", + "\n", + "print(word_freq2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## The remaining of this lab is optional and we recommend that you revisit it after we cover the appropriate material:" ] }, { @@ -182,7 +218,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -196,9 +232,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.11.7" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 2e59d77..642e6b1 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -2,11 +2,13 @@ "cells": [ { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, "source": [ - "## Challenge 1: Tuples\n", + "Challenge 1: Tuples\n", "\n", - "#### Do you know you can create tuples with only one element?\n", + "Do you know you can create tuples with only one element?\n", "\n", "**In the cell below, define a variable `tup` with a single element `\"I\"`.**\n", "\n", @@ -15,11 +17,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "tup = (\"I\", )" ] }, { @@ -33,11 +35,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup = (\"I\",) \n", + "print(type(tup))" ] }, { @@ -55,13 +66,26 @@ }, { "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" + ] + } + ], "source": [ - "# Your code here\n", + "tup = (\"I\",) \n", + "new_elements = (\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") \n", + "\n", + "new_tup = tup + new_elements\n", "\n", - "# Your explanation here\n" + "print(new_tup)\n", + "#tuples are immutable, which means once they are created, their content cannot be changed.Adding or removing elements, which is not possible directly. Therefore, we cannot append elements to tup or any other tuple once it has been defined.\n", + "#However, if we need a tuple that includes the additional elements, we can create a new tuple that combines tup with these new elements.\n" ] }, { @@ -79,13 +103,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n", + "tup = (\"I\", )\n", + "tup = (\"I\",\"r\",\"o\",\"h\",\"a\",\"c\",\"k\")\n", + "print(tup)\n", "\n", - "# Your explanation here\n" + "# Yes we can reassign the existing top\n" ] }, { @@ -103,11 +137,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "tup1: ('I', 'r', 'o', 'n')\n", + "tup2: ('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup = (\"I\", \"r\", \"o\", \"n\",\"h\", \"a\", \"c\", \"k\")\n", + "print(tup)\n", + "tup1 = tup[0:4] \n", + "tup2 = tup[-4:] \n", + "print(\"tup1:\", tup1)\n", + "print(\"tup2:\", tup2)\n" ] }, { @@ -121,11 +170,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tup3: ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "tup3 equals to tup.\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup1 = (\"I\", \"r\", \"o\", \"n\")\n", + "tup2 = (\"h\", \"a\", \"c\", \"k\")\n", + "tup3 = tup1 + tup2\n", + "print(\"tup3:\", tup3)\n", + "original_tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "if tup3 == original_tup:\n", + " print(\"tup3 equals to tup.\")\n", + "else:\n", + " print(\"tup3 does not equal to tup.\")\n" ] }, { @@ -137,34 +203,61 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The sum of elements in tup1 and tup2 is the same as the number of elements in tup3.\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup3 = tup1 + tup2\n", + "count_tup1 = len(tup1) \n", + "count_tup2 = len(tup2)\n", + "sum_counts = count_tup1 + count_tup2\n", + "count_tup3 = len(tup3)\n", + "\n", + "if sum_counts == count_tup3:\n", + " print(\"The sum of elements in tup1 and tup2 is the same as the number of elements in tup3.\")\n", + "else:\n", + " print(\"The sum of elements in tup1 and tup2 is not the same as the number of elements in tup3.\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### What is the index number of `\"h\"` in `tup3`?" + "What is the index number of `\"h\"` in `tup3`?" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The index of 'h' in tup3 is: 4\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup3 = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") \n", + "index_of_h = tup3.index(\"h\")\n", + "print(\"The index of 'h' in tup3 is:\", index_of_h)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Now, use a FOR loop to check whether each letter in the following list is present in `tup3`:\n", + "Now, use a FOR loop to check whether each letter in the following list is present in `tup3`:\n", "\n", "```\n", "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", @@ -177,11 +270,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "False\n", + "False\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup3 = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") \n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "for letter in letters:\n", + " if letter in tup3:\n", + " print(True)\n", + " else:\n", + " print(False)\n" ] }, { @@ -195,17 +307,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The letter 'a' appears 1 times in tup3.\n", + "The letter 'b' appears 0 times in tup3.\n", + "The letter 'c' appears 1 times in tup3.\n", + "The letter 'd' appears 0 times in tup3.\n", + "The letter 'e' appears 0 times in tup3.\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup3 = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") \n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"] \n", + "for letter in letters:\n", + " count = tup3.count(letter)\n", + " print(f\"The letter '{letter}' appears {count} times in tup3.\")\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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,9 +361,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.11.7" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index cb3a3e0..7db252d 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -38,11 +38,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[57, 34, 28, 27, 95, 100, 49, 61, 25, 29, 59, 46, 85, 3, 10, 17, 56, 44, 58, 66, 74, 70, 7, 69, 5, 93, 31, 75, 99, 8, 68, 48, 41, 86, 50, 72, 22, 43, 73, 52, 24, 77, 82, 78, 15, 6, 35, 40, 39, 91, 96, 79, 87, 98, 71, 84, 1, 51, 0, 20, 11, 65, 80, 64, 42, 97, 9, 45, 62, 12, 47, 37, 81, 30, 18, 89, 38, 54, 4, 36]\n" + ] + } + ], "source": [ - "# Your code here\n" + "import random\n", + "sample_list_1 = random.sample(range(101), 80) \n", + "print(sample_list_1)\n" ] }, { @@ -54,18 +64,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The length of set1 is: 80\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = set(sample_list_1)\n", + "print(\"The length of set1 is:\", len(set1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### 3 - 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,11 +96,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[50, 40, 84, 52, 72, 60, 55, 1, 75, 7, 4, 53, 46, 17, 97, 23, 91, 37, 100, 36, 20, 21, 27, 90, 18, 20, 44, 7, 14, 20, 43, 42, 56, 74, 9, 74, 67, 29, 36, 99, 20, 70, 13, 27, 10, 77, 62, 45, 72, 74, 87, 39, 84, 80, 13, 17, 7, 17, 3, 50, 95, 91, 18, 61, 0, 82, 30, 15, 65, 23, 58, 57, 83, 43, 20, 9, 57, 33, 54, 83]\n" + ] + } + ], "source": [ - "# Your code here\n" + "import random\n", + "sample_list_2 = []\n", + "for _ in range(80):\n", + " number = random.randint(0, 100)\n", + " sample_list_2.append(number)\n", + "print(sample_list_2)" ] }, { @@ -93,27 +125,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The length of set2 is: 57\n" + ] + } + ], "source": [ - "# Your code here\n" + "set2 = set(sample_list_2)\n", + "print(\"The length of set2 is:\", len(set2))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### 5 - 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": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, 6, 8, 11, 12, 22, 24, 25, 28, 31, 34, 35, 38, 41, 47, 48, 49, 51, 59, 64, 66, 68, 69, 71, 73, 78, 79, 81, 85, 86, 89, 93, 96, 98}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = set(sample_list_1)\n", + "set2 = set(sample_list_2)\n", + "set3 = set1 - set2\n", + "print(set3)" ] }, { @@ -125,11 +177,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{33, 67, 13, 14, 83, 21, 53, 23, 55, 90, 60}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = set(sample_list_1)\n", + "set2 = set(sample_list_2)\n", + "set4 = set2 - set1\n", + "print(set4)" ] }, { @@ -141,27 +204,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 3, 4, 7, 9, 10, 15, 17, 18, 20, 27, 29, 30, 36, 37, 39, 40, 42, 43, 44, 45, 46, 50, 52, 54, 56, 57, 58, 61, 62, 65, 70, 72, 74, 75, 77, 80, 82, 84, 87, 91, 95, 97, 99, 100}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = set(sample_list_1)\n", + "set2 = set(sample_list_2)\n", + "set5 = set1.intersection(set2)\n", + "print(set5)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### 8 - Create an empty set called `set6`." + " 8 - Create an empty set called `set6`." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n", + "\n" + ] + } + ], + "source": [ + "set6 = set()\n", + "\n", + "print(set6)\n", + "print(type(set6))\n" ] }, { @@ -173,11 +259,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 17, 18, 20, 22, 24, 25, 27, 28, 29, 30, 31, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 56, 57, 58, 59, 61, 62, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 89, 91, 93, 95, 96, 97, 98, 99, 100}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set3 = set1 - set2\n", + "set5 = set1.intersection(set2)\n", + "set6 = set() \n", + "set6.update(set3)\n", + "set6.update(set5)\n", + "print(set6)\n", + " " ] }, { @@ -189,11 +289,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ - "# Your code here\n" + "are_equal = set1 == set6\n", + "print(are_equal)\n" ] }, { @@ -205,52 +314,92 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set1 contains set2: False\n", + "set1 contains set3: True\n" + ] + } + ], + "source": [ + "set1 = set(sample_list_1)\n", + "set2 = set(sample_list_2)\n", + "contains_set2 = set2.issubset(set1)\n", + "print(f\"set1 contains set2: {contains_set2}\")\n", + "contains_set3 = set3.issubset(set1)\n", + "print(f\"set1 contains set3: {contains_set3}\")\n", + "\n", + "\n", + "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### 12 -Using the Python Set `union` method, aggregate `set3`, `set4`, and `set5`. Then aggregate `set1` and `set2`. \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." + " Check if the aggregated values are equal." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The aggregated values are equal: True\n" + ] + } + ], "source": [ - "# Your code here\n" + "aggregate1 = set3.union(set4, set5)\n", + "aggregate2 = set1.union(set2)\n", + "are_aggregates_equal = aggregate1 == aggregate2\n", + "print(f\"The aggregated values are equal: {are_aggregates_equal}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### 13 -Using the `pop` method, remove the first element from `set1`." + " 13 -Using the `pop` method, remove the first element from `set1`." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Removed element: 1\n", + "Remaining set: {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 17, 18, 20, 22, 24, 25, 27, 28, 29, 30, 31, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 56, 57, 58, 59, 61, 62, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 89, 91, 93, 95, 96, 97, 98, 99, 100}\n" + ] + } + ], + "source": [ + "removed_element = set1.pop()\n", + "\n", + "print(f\"Removed element: {removed_element}\")\n", + "print(f\"Remaining set: {set1}\")" ] }, { "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", + " 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", @@ -259,28 +408,46 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Remaining elements in set1: {3, 4, 5, 6, 7, 8, 10, 12, 15, 17, 18, 20, 22, 24, 25, 27, 28, 30, 34, 35, 36, 37, 38, 40, 42, 43, 44, 45, 46, 47, 48, 50, 52, 54, 56, 57, 58, 62, 64, 65, 66, 68, 70, 72, 73, 74, 75, 77, 78, 80, 82, 84, 85, 86, 87, 93, 95, 96, 97, 98, 100}\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", + "for item in list_to_remove:\n", + " set1.discard(item)\n", + "print(f\"Remaining elements in set1: {set1}\")" ] }, { "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", + "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 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, - "metadata": {}, - "outputs": [], + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unique Grocery List: ['cucumber', 'sugar', 'garlic', 'juice', 'flour', 'gums', 'toilet paper', 'cherries', 'onions', 'water', 'blueberries', 'tomatoes', 'watermelon', 'milk', 'yogurt']\n" + ] + } + ], "source": [ "father_list = ['garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions', 'gums', 'flour', 'cucumber', 'watermelon', 'yogurt', 'garlic']\n", "\n", @@ -292,7 +459,16 @@ "\n", "Mattia_list = []\n", "\n", - "\n" + "\n", + "\n", + "# Combine lists into one large list\n", + "combined_list = father_list + mother_list + Jo_list + Carlos_list + Mattia_list\n", + "\n", + "# Convert the combined list to a set to remove duplicates\n", + "unique_grocery_set = set(combined_list)\n", + "\n", + "unique_grocery_list = list(unique_grocery_set)\n", + "print(\"Unique Grocery List:\", unique_grocery_list)\n" ] }, { @@ -304,11 +480,47 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Father's unique items: {'cucumber', 'garlic', 'flour', 'gums', 'toilet paper', 'onions', 'watermelon', 'yogurt'}\n", + "Mother's unique items: set()\n", + "Jo's unique items: {'cucumber', 'sugar', 'garlic', 'juice', 'gums', 'onions', 'blueberries', 'tomatoes', 'watermelon', 'milk', 'yogurt'}\n", + "Carlos's unique items: {'garlic', 'flour', 'cherries', 'toilet paper', 'onions', 'water', 'tomatoes', 'blueberries'}\n", + "Mattia's unique items: set()\n" + ] + } + ], + "source": [ + "#Approach 1 - Direct Method\n", + "father_set = set(father_list)\n", + "mother_set = set() # Mother's and Mattia's lists are empty\n", + "Jo_set = set(Jo_list)\n", + "Carlos_set = set(Carlos_list)\n", + "Mattia_set = set(Mattia_list)\n", + "\n", + "print(\"Father's unique items:\", father_set)\n", + "print(\"Mother's unique items:\", mother_set)\n", + "print(\"Jo's unique items:\", Jo_set)\n", + "print(\"Carlos's unique items:\", Carlos_set)\n", + "print(\"Mattia's unique items:\", Mattia_set)\n", + "\n", + "\n", + "#Approach 2 - set comparision\n", + "father_set = {item for item in father_list}\n", + "mother_set = {item for item in mother_list}\n", + "Jo_set = {item for item in Jo_list}\n", + "Carlos_set = {item for item in Carlos_list}\n", + "Mattia_set = {item for item in Mattia_list}\n", + "print(\"Father's unique items:\", father_set)\n", + "print(\"Mother's unique items:\", mother_set)\n", + "print(\"Jo's unique items:\", Jo_set)\n", + "print(\"Carlos's unique items:\", Carlos_set)\n", + "print(\"Mattia's unique items:\", Mattia_set)\n" ] }, { @@ -322,11 +534,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mattia's unique items: {'cucumber', 'sugar', 'juice', 'flour', 'gums', 'cherries', 'toilet paper', 'water', 'watermelon', 'milk', 'yogurt'}\n" + ] + } + ], + "source": [ + "unique_to_Carlos = Carlos_set - Jo_set\n", + "\n", + "unique_to_Jo = Jo_set - Carlos_set\n", + "\n", + "Mattia_set = unique_to_Carlos | unique_to_Jo\n", + "\n", + "print(\"Mattia's unique items:\", Mattia_set)" ] }, { @@ -338,9 +564,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['shampoo', 'chocolate', 'garlic', 'milk', 'water', 'shampoo', 'bread', 'yogurt', 'water', 'flour']\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 +586,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mother (unique items): ['chocolate', 'garlic', 'flour', 'water', 'shampoo', 'bread', 'milk', 'yogurt']\n" + ] + } + ], "source": [ - "# Your code here\n" + "mother = list(set(mother_list))\n", + "print(\"Mother (unique items):\", mother)" ] }, { @@ -373,11 +616,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Father's items: {'onions', 'garlic', 'watermelon', 'yogurt'}\n", + "Mother's items: {'water', 'chocolate', 'milk'}\n", + "Jo's items: {'blueberries', 'sugar', 'watermelon'}\n", + "Carlos's items: {'onions', 'water', 'tomatoes', 'blueberries'}\n", + "Mattia's items: {'cherries', 'sugar'}\n" + ] + } + ], + "source": [ + "#Recreating faimly set\n", + "father_set = {'garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions'}\n", + "mother_set = {'milk', 'water', 'chocolate', 'toilet paper'} \n", + "Jo_set = {'blueberries', 'sugar', 'watermelon', 'toilet paper'}\n", + "Carlos_set = {'tomatoes', 'water', 'onions', 'blueberries', 'toilet paper'}\n", + "Mattia_set = {'sugar', 'cherries', 'toilet paper'} \n", + "family_sets = [father_set, mother_set, Jo_set, Carlos_set, Mattia_set]\n", + "\n", + "for family_set in family_sets:\n", + " family_set.discard('toilet paper')\n", + "\n", + "print(\"Father's items:\", father_set)\n", + "print(\"Mother's items:\", mother_set)\n", + "print(\"Jo's items:\", Jo_set)\n", + "print(\"Carlos's items:\", Carlos_set)\n", + "print(\"Mattia's items:\", Mattia_set)\n" ] }, { @@ -389,20 +659,56 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unique Grocery List: {'chocolate', 'cucumber', 'sugar', 'garlic', 'juice', 'bread', 'flour', 'gums', 'toilet paper', 'cherries', 'onions', 'water', 'shampoo', 'blueberries', 'tomatoes', 'watermelon', 'milk', 'yogurt'}\n" + ] + } + ], + "source": [ + "combined_list = father_list + mother_list + Jo_list + Carlos_list + Mattia_list\n", + "\n", + "# Convert the combined list to a set to remove duplicates\n", + "unique_grocery_set = set(combined_list)\n", + "\n", + "print(\"Unique Grocery List:\", unique_grocery_set)\n" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your code here\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", + "Hint: use the `.pop()` method." ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 46, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Father will buy: ['onions', 'yogurt', 'garlic', 'watermelon', 'toilet paper']\n", + "Items left in the set: {'oil', 'milk'}\n" + ] + } + ], "source": [ - "#### 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", - "##### Hint: use the `.pop()` method." + "father_set = {'garlic', 'watermelon', 'toilet paper', 'yogurt', 'onions','milk', 'oil'}\n", + "items_to_buy = []\n", + "for _ in range(5):\n", + " item = father_set.pop()\n", + " items_to_buy.append(item)\n", + "print(\"Father will buy:\", items_to_buy)\n", + "print(\"Items left in the set:\", father_set)" ] }, { @@ -410,14 +716,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" }, @@ -431,9 +735,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.11.7" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index e041c1d..4cd0de8 100644 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -49,18 +49,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "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" + "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", + "keys = list(word_freq.keys())\n", + "keys.sort()\n", + "\n", + "word_freq2 = {}\n", + "\n", + "for key in keys:\n", + " value = word_freq[key]\n", + " word_freq2[key] = value\n", + "\n", + "print(word_freq2)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Sort the values of `word_freq` ascendingly.\n", + " Sort the values of `word_freq` ascendingly.\n", "\n", "Sorting the values of a dictionary is more tricky than sorting the keys because a dictionary's values are not unique. Therefore you cannot use the same way you sorted dict keys to sort dict values.\n", "\n", @@ -90,11 +108,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "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", + "{('conversation', 1): 21, (\"we're\", 1): 21, ('plate', 1): 21, ('sour', 1): 21, ('jukebox', 1): 21, ('taxi', 1): 21, ('fast', 1): 21, ('bag', 1): 21, ('man', 1): 21, ('going', 1): 21, ('one', 1): 21, ('backseat', 1): 21, ('friends', 1): 21, ('take', 1): 21, ('play', 1): 21, ('okay', 1): 21, ('begin', 1): 21, ('over', 1): 21, ('just', 1): 21, ('are', 1): 21, ('tell', 1): 21, ('drinking', 1): 21, ('our', 1): 21, ('where', 1): 21, (\"i'll\", 1): 21, ('all', 1): 21, (\"isn't\", 1): 21, ('make', 1): 21, ('lover', 1): 21, ('get', 1): 21, ('radio', 1): 21, ('give', 1): 21, ('can', 1): 21, ('club', 1): 21, ('it', 1): 21, ('out', 1): 21, ('chance', 1): 21, ('first', 1): 21, ('table', 1): 21, ('thrifty', 1): 21, ('driver', 1): 21, ('slow', 1): 21, ('dance', 1): 21, ('trust', 1): 21, ('family', 1): 21, ('week', 1): 21, ('date', 1): 21, ('leave', 1): 21, ('at', 1): 21, ('hand', 1): 21, ('how', 1): 21, ('eat', 1): 21, ('about', 1): 21, ('story', 1): 21, ('sweet', 1): 21, ('best', 1): 21, ('let', 1): 21, ('van', 1): 21, ('shots', 1): 21, ('place', 1): 21, ('find', 1): 21, ('kiss', 1): 21, ('stop', 1): 21, ('bar', 1): 21, (\"don't\", 2): 21, ('mind', 2): 21, ('know', 2): 21, ('so', 2): 21, ('start', 2): 21, ('boy', 2): 21, ('girl', 2): 21, ('singing', 2): 21, ('doing', 2): 21, ('somebody', 2): 21, ('handmade', 2): 21, ('may', 2): 21, ('that', 2): 21, ('much', 2): 21, ('grab', 2): 21, ('was', 2): 21, ('say', 2): 21, ('waist', 2): 21, ('want', 2): 21, (\"let's\", 2): 21, ('not', 2): 21, ('crazy', 2): 21, ('go', 2): 21, ('to', 2): 21, ('fill', 2): 21, ('hours', 2): 21, ('push', 3): 21, ('then', 3): 21, ('put', 3): 21, ('room', 3): 21, ('magnet', 3): 21, ('up', 3): 21, ('pull', 3): 21, ('last', 3): 21, ('do', 3): 21, ('smell', 3): 21, ('although', 3): 21, ('falling', 3): 21, ('were', 3): 21, ('night', 3): 21, ('heart', 3): 21, ('for', 3): 21, ('bedsheets', 3): 21, ('talk', 4): 21, ('too', 5): 21, ('is', 5): 21, ('every', 6): 21, ('new', 6): 21, ('follow', 6): 21, ('brand', 6): 21, ('of', 6): 21, ('i', 6): 21, ('day', 6): 21, ('lead', 6): 21, ('shape', 6): 21, ('discovering', 6): 21, ('something', 6): 21, ('we', 7): 21, ('a', 8): 21, ('like', 10): 21, ('me', 10): 21, ('now', 11): 21, ('baby', 14): 21, ('you', 16): 21, ('be', 16): 21, ('body', 17): 21, ('the', 18): 21, ('your', 21): 21, ('with', 22): 21, (\"i'm\", 23): 21, ('and', 23): 21, ('love', 25): 21, ('in', 27): 21, ('my', 33): 21, ('come', 37): 21, ('on', 40): 21}\n" + ] + } + ], "source": [ - "# Your code here\n" + "import operator\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "print(sorted_tups)\n", + "word_freq2 = {}\n", + "\n", + "for key in sorted_tups:\n", + " word_freq2[key] = value\n", + "\n", + "print(word_freq2)" ] }, { @@ -183,7 +218,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -197,9 +232,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.8" + "version": "3.11.7" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }