diff --git a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb index b65ffd1..10d6021 100644 --- a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -15,12 +15,24 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "('I',)" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here\n", - "tup = (\"I\",)" + "tup = (\"I\",)\n", + "tup" ] }, { @@ -34,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -43,14 +55,14 @@ "tuple" ] }, - "execution_count": 2, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Your code here\n", - "type(tup)" + "type(tup)\n" ] }, { @@ -68,26 +80,25 @@ }, { "cell_type": "code", - "execution_count": 3, - "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'" - ] - } - ], + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], "source": [ "# Your code here\n", "tup.append(\"r\")\n", "# Your explanation here\n", - "# tuple is immutable" + "\"\"\"\n", + "Trying to append an element to a tuple with the code above will result in an AttributeError.\n", + "This is because tuples in Python are inmutable, , meaning once they are created, their contents cannot be changed. \n", + "Since tuples are immutable, you can also no add, or remove elements from them after creation.\n", + "\n", + "\"\"\"\n", + "\n", + "\n", + "\n", + "\n" ] }, { @@ -110,9 +121,14 @@ "outputs": [], "source": [ "# Your code here\n", - "tup[0] = \"r\"\n", + "#tup[1] = \"r\" - this would give me an error\n", + "\n", + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") #instead I reassign a new tuple to tup.\n", "# Your explanation here\n", - "# tuple is immutable" + "\"\"\"\n", + "Because tuples are immutable, I can't directly reassign individual elements of a tuple\n", + "I can, however, as I did above, create a new tuple and assign it to tup, as an alternative\n", + "\"\"\"\n" ] }, { @@ -132,11 +148,21 @@ "cell_type": "code", "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n') ('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ "# Your code here\n", - "tup1 = (\"I\", \"r\", \"o\", \"n\")\n", - "tup2 = (\"h\", \"a\", \"c\", \"k\")" + "tup1 = tup[:4]\n", + "tup2 = tup[-4:]\n", + "\n", + "print(tup1, tup2)" ] }, { @@ -154,22 +180,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" - ] + "data": { + "text/plain": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "# Your code here\n", - "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", "tup3 = tup1 + tup2\n", - "print(tup3)\n", - "print(tup)\n", - "print(tup == tup3)" + "tup3" ] }, { @@ -181,7 +205,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -189,18 +213,27 @@ "output_type": "stream", "text": [ "4\n", - "8\n", - "8\n", - "8\n" + "4\n" ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "# Your code here\n", - "print(len(tup1))\n", - "print(len(tup3))\n", - "print(len(tup1) + len(tup2))\n", - "print(len(tup3))" + "print(len(tup1)) #print lenght of tup1\n", + "print(len(tup2)) #print lenght of tup1\n", + "sum_len = len(tup1) + len(tup2) #sum the lengths of tup1 ans tup2 and assign it to sum_len\n", + "\n", + "sum_len == len(tup3) #check if sum_len is equal to the length of tup3" ] }, { @@ -212,7 +245,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -221,15 +254,14 @@ "4" ] }, - "execution_count": 32, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Your code here\n", - "tup3.index(\"h\")\n", - "##It's number 4" + "tup.index(\"h\")" ] }, { @@ -249,33 +281,26 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 18, "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", - "\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" + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "for letter in letters:\n", + " print(letter in tup3)\n" ] }, { @@ -289,18 +314,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Letter a appears 1 time(s) in tup3.\n", + "Letter c appears 1 time(s) in tup3.\n" + ] + } + ], "source": [ "# Your code here\n", - "\n" + "for letter in letters:\n", + " if letter in tup3:\n", + " print(f\"Letter {letter} appears {tup3.count(letter)} time(s) in tup3.\")" ] + }, + { + "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 +357,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..c1e9e4e 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": 1, "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,138 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[79,\n", + " 50,\n", + " 70,\n", + " 85,\n", + " 96,\n", + " 65,\n", + " 72,\n", + " 28,\n", + " 94,\n", + " 14,\n", + " 34,\n", + " 97,\n", + " 23,\n", + " 80,\n", + " 44,\n", + " 49,\n", + " 18,\n", + " 6,\n", + " 98,\n", + " 41,\n", + " 67,\n", + " 5,\n", + " 73,\n", + " 21,\n", + " 64,\n", + " 63,\n", + " 43,\n", + " 54,\n", + " 90,\n", + " 20,\n", + " 25,\n", + " 59,\n", + " 78,\n", + " 93,\n", + " 47,\n", + " 12,\n", + " 87,\n", + " 40,\n", + " 31,\n", + " 83,\n", + " 10,\n", + " 89,\n", + " 68,\n", + " 88,\n", + " 8,\n", + " 60,\n", + " 15,\n", + " 56,\n", + " 45,\n", + " 77,\n", + " 46,\n", + " 62,\n", + " 2,\n", + " 38,\n", + " 74,\n", + " 27,\n", + " 48,\n", + " 1,\n", + " 32,\n", + " 4,\n", + " 91,\n", + " 86,\n", + " 16,\n", + " 42,\n", + " 76,\n", + " 26,\n", + " 37,\n", + " 11,\n", + " 99,\n", + " 95,\n", + " 100,\n", + " 36,\n", + " 17,\n", + " 24,\n", + " 61,\n", + " 35,\n", + " 75,\n", + " 52,\n", + " 7,\n", + " 57]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "sample_list_1 = random.sample(range(101),k=80)\n", + "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, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The length of set1 is 80 and it is True that it is the same as the length of sample_list_1\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set1 = set(sample_list_1)\n", + "len_set = len(set1)\n", + "\n", + "print(f\"The length of set1 is {len_set} and it is {len_set == len(sample_list_1)} that it is the same as the length of sample_list_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", @@ -81,219 +185,641 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "sample_list_2 = [random.randint(0,100) for i in range(80)]\n", + "\n", + "sample_list_2\n", + "\n" ] }, { "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": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "51\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set2 = set(sample_list_2)\n", + "print(len(set2)) #this is no longer 80 because sets ignore the duplicate elements in sample_list_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": 19, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set3 = set1.difference(set2)\n" ] }, { "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": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{3, 9, 29, 39, 53, 55, 66, 71, 82, 92}" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set4 = set2.difference(set1)\n", + "set4\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 7 - Now Identify the elements shared between `set1` and `set2`. Assign the elements to a new set named `set5`." + ] + }, + { + "cell_type": "code", + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set5 = set1.intersection(set2)\n", + "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Now Identify the elements shared between `set1` and `set2`. Assign the elements to a new set named `set5`." + "#### 8 - Create an empty set called `set6`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set6 = set()" ] }, { "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", + "#### 9 - Add `set3` and `set5` to `set6` using the Python Set `update` method." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1,\n", + " 2,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 10,\n", + " 11,\n", + " 12,\n", + " 14,\n", + " 15,\n", + " 16,\n", + " 17,\n", + " 18,\n", + " 20,\n", + " 21,\n", + " 23,\n", + " 24,\n", + " 25,\n", + " 26,\n", + " 27,\n", + " 28,\n", + " 31,\n", + " 32,\n", + " 34,\n", + " 35,\n", + " 36,\n", + " 37,\n", + " 38,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 49,\n", + " 50,\n", + " 52,\n", + " 54,\n", + " 56,\n", + " 57,\n", + " 59,\n", + " 60,\n", + " 61,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 67,\n", + " 68,\n", + " 70,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 75,\n", + " 76,\n", + " 77,\n", + " 78,\n", + " 79,\n", + " 80,\n", + " 83,\n", + " 85,\n", + " 86,\n", + " 87,\n", + " 88,\n", + " 89,\n", + " 90,\n", + " 91,\n", + " 93,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97,\n", + " 98,\n", + " 99,\n", + " 100}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set6.update(set3,set5)\n", + "set6" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 10 - Check if `set1` and `set6` are equal." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1 == set6" + ] + }, + { + "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": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False True\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(set2.issubset(set1),set3.issubset(set1))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 12 -Using the Python Set `union` method, aggregate `set3`, `set4`, and `set5`. Then aggregate `set1` and `set2`. \n", "\n", - "Use a math formular to represent that relationship. Test your formular with Python code." + "#### Check if the aggregated values are equal." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set7 = set3.union(set4,set5)\n", + "set8 = set1.union(set2)\n", + "set7 == set8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Create an empty set called `set6`." + "#### 13 -Using the `pop` method, remove the first element from `set1`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "#Since sets in Python are unordered collections, there is no concept of a \"first\" element in a set. \n", + "#Therefore, removing the \"first\" element from a set using the pop() method doesn't make sense in the traditional sense as it does for lists.\n", + "#I could use set1.pop() to remove an arbitrary element from the set, or convert set 1 into a list to temporarily remove the first element.\n", + "#I will use the option to covnert the set into a list\n", + "\n", + "list1 = list(set1)" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 46, "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#### Add `set3` and `set5` to `set6` using the Python Set `update` method." + "list1.pop(0)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "set1 = set(list1)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Check if `set1` and `set6` are equal." + "#### 14 -Remove every element in the following list from `set1` if they are present in the set. Print the remaining elements.\n", + "\n", + "```\n", + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "```" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 4, 5, 6, 7, 8, 10, 12, 14, 15, 16, 17, 18, 20, 23, 24, 25, 26, 27, 28, 32, 34, 35, 36, 37, 38, 40, 42, 43, 44, 45, 46, 47, 48, 50, 52, 54, 56, 57, 60, 62, 63, 64, 65, 67, 68, 70, 72, 73, 74, 75, 76, 77, 78, 80, 83, 85, 86, 87, 88, 90, 93, 94, 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", + "\n", + "for element in list_to_remove:\n", + " if element in set1:\n", + " set1.remove(element)\n", + "\n", + "print(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", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 54, "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": [ + "#### 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": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Father's set: {'watermelon', 'cucumber', 'gums', 'yogurt', 'onions', 'flour', 'garlic', 'toilet paper'}\n", + "Mother's set: set()\n", + "Jo's set: {'watermelon', 'cucumber', 'juice', 'sugar', 'milk', 'gums', 'yogurt', 'onions', 'blueberries', 'garlic', 'tomatoes'}\n", + "Carlos's set: {'cherries', 'blueberries', 'onions', 'flour', 'water', 'garlic', 'toilet paper', 'tomatoes'}\n", + "Mattia's set: set()\n" + ] + } + ], + "source": [ + "# 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)\n", + "\n", + "print(\"Father's set:\", father_set)\n", + "print(\"Mother's set:\", mother_set)\n", + "print(\"Jo's set:\", Jo_set)\n", + "print(\"Carlos's set:\", Carlos_set)\n", + "print(\"Mattia's set:\", Mattia_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Check if `set1` contains `set2` using the Python Set `issubset` method. Then check if `set1` contains `set3`.*" + "#### 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", + "##### Hint: check out the documentation for `x.symmetric_difference(y)`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Mattia's set: {'watermelon', 'cucumber', 'juice', 'sugar', 'cherries', 'milk', 'gums', 'yogurt', 'flour', 'water', 'toilet paper'}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "Mattia_set = Carlos_set.symmetric_difference(Jo_set)\n", + "print(\"Updated Mattia's set:\", Mattia_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Using the Python Set `union` method, aggregate `set3`, `set4`, and `set5`. Then aggregate `set1` and `set2`. \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": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['cucumber', 'milk', 'mushrooms', 'onions', 'bread', 'yogurt', 'mushrooms', 'chocolate', 'bread', 'onions']\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", - "#### Check if the aggregated values are equal." + "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": null, + "execution_count": 58, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'bread', 'chocolate', 'cucumber', 'milk', 'mushrooms', 'onions', 'yogurt'}" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "mother_set = {item for item in mother_list} #using another method here than above, for variation\n", + "mother_set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Using the `pop` method, remove the first element from `set1`." + "#### 15.4- Before leaving the house to the grocery store the father found a lot of toilet paper, so the mother decided to remove it from the items needed to be bought. Use the `.discard()` method to remove from each set created before the 'toilet paper'. \n", + "\n", + "#### Hint: Try to first create a list contaning the variables of each set created before, then loop through them and remove the unecessary item. \n", + "\n", + "##### Note: The`.discard()` method will remove and update the set without the need of reassignment.\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "sets_list = [father_set, mother_set, Jo_set, Carlos_set, Mattia_set]\n", + "\n", + "for s in sets_list:\n", + " s.discard(\"toilet paper\")" ] }, { "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.5- Create a set with all the groceries needed to be bought for the house (no repetitions)." + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'watermelon', 'juice', 'sugar', 'cherries', 'gums', 'onions', 'flour', 'bread', 'blueberries', 'tomatoes', 'water', 'cucumber', 'mushrooms', 'milk', 'yogurt', 'chocolate', 'garlic'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "groceries_set = mother_set.union(father_set, Jo_set, Carlos_set, Mattia_set)\n", + "print(groceries_set)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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", - "```\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": null, + "execution_count": 63, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Bought: watermelon\n", + "Bought: juice\n", + "Bought: sugar\n", + "Bought: cherries\n", + "Bought: gums\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "for i in range(5):\n", + " item_bought = groceries_set.pop()\n", + " print(\"Bought:\",item_bought)\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -307,9 +833,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..77a0b19 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": 8, "metadata": {}, "outputs": [], "source": [ @@ -49,17 +49,26 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 9, "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 = list(word_freq.keys())\n", + "keys_list.sort()\n", "word_freq2 = {}\n", + "for element in keys_list:\n", + " word_freq2[element] = word_freq[element]\n", "\n", - "for key in keys.keys():\n", - " " + "print(word_freq2)" ] }, { @@ -96,11 +105,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, + "metadata": {}, + "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", + "import operator\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "print(sorted_tups)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "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": [ + "word_freq2 = {}\n", + "for key, value in sorted_tups:\n", + " word_freq2[key] = value\n", + "print(word_freq2)" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your code here\n" + "## The remaining of this lab is optional and we recommend that you revisit it after we cover the appropriate material:" ] }, { @@ -116,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -138,11 +185,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "0 love 25\n", + "1 conversation 1\n", + "2 every 6\n", + "3 we're 1\n", + "4 plate 1\n", + ".. ... ...\n", + "135 bedsheets 3\n", + "136 fill 2\n", + "137 hours 2\n", + "138 stop 1\n", + "139 bar 1\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df = pd.DataFrame(list(word_freq.items()), columns=['word', 'freq'])\n", + "print(df)\n", + "\n", + "# list(word_freq.items()) is used to prepare the data in a compatible format for the DataFrame constructor, ensuring that each key-value pair from the dictionary becomes a row in the DataFrame." ] }, { @@ -156,11 +228,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "120 a 8\n", + "109 about 1\n", + "43 all 1\n", + "96 although 3\n", + "72 and 23\n", + ".. ... ...\n", + "128 were 3\n", + "41 where 1\n", + "54 with 22\n", + "15 you 16\n", + "97 your 21\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by = 'word', ascending = True, inplace = True)\n", + "print(df)" ] }, { @@ -172,17 +267,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "114 let 1\n", + "87 trust 1\n", + "11 man 1\n", + "45 make 1\n", + "46 lover 1\n", + ".. ... ...\n", + "0 love 25\n", + "65 in 27\n", + "121 my 33\n", + "56 come 37\n", + "126 on 40\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by = 'freq', ascending = True, inplace = True)\n", + "print(df)" ] + }, + { + "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,9 +321,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..10d6021 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "('I',)" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup = (\"I\",)\n", + "tup" ] }, { @@ -33,11 +46,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)\n" ] }, { @@ -56,12 +81,24 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [], "source": [ "# Your code here\n", + "tup.append(\"r\")\n", + "# Your explanation here\n", + "\"\"\"\n", + "Trying to append an element to a tuple with the code above will result in an AttributeError.\n", + "This is because tuples in Python are inmutable, , meaning once they are created, their contents cannot be changed. \n", + "Since tuples are immutable, you can also no add, or remove elements from them after creation.\n", + "\n", + "\"\"\"\n", + "\n", "\n", - "# Your explanation here\n" + "\n", + "\n" ] }, { @@ -84,8 +121,14 @@ "outputs": [], "source": [ "# Your code here\n", + "#tup[1] = \"r\" - this would give me an error\n", "\n", - "# Your explanation here\n" + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") #instead I reassign a new tuple to tup.\n", + "# Your explanation here\n", + "\"\"\"\n", + "Because tuples are immutable, I can't directly reassign individual elements of a tuple\n", + "I can, however, as I did above, create a new tuple and assign it to tup, as an alternative\n", + "\"\"\"\n" ] }, { @@ -103,11 +146,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n') ('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup1 = tup[:4]\n", + "tup2 = tup[-4:]\n", + "\n", + "print(tup1, tup2)" ] }, { @@ -121,11 +176,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup3 = tup1 + tup2\n", + "tup3" ] }, { @@ -137,11 +205,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "4\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(len(tup1)) #print lenght of tup1\n", + "print(len(tup2)) #print lenght of tup1\n", + "sum_len = len(tup1) + len(tup2) #sum the lengths of tup1 ans tup2 and assign it to sum_len\n", + "\n", + "sum_len == len(tup3) #check if sum_len is equal to the length of tup3" ] }, { @@ -153,11 +245,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup.index(\"h\")" ] }, { @@ -177,11 +281,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "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", + "for letter in letters:\n", + " print(letter in tup3)\n" ] }, { @@ -195,17 +314,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Letter a appears 1 time(s) in tup3.\n", + "Letter c appears 1 time(s) in tup3.\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "for letter in letters:\n", + " if letter in tup3:\n", + " print(f\"Letter {letter} appears {tup3.count(letter)} time(s) in tup3.\")" ] + }, + { + "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 +357,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..c1e9e4e 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": 1, "metadata": {}, "outputs": [], "source": [ @@ -38,11 +38,103 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[79,\n", + " 50,\n", + " 70,\n", + " 85,\n", + " 96,\n", + " 65,\n", + " 72,\n", + " 28,\n", + " 94,\n", + " 14,\n", + " 34,\n", + " 97,\n", + " 23,\n", + " 80,\n", + " 44,\n", + " 49,\n", + " 18,\n", + " 6,\n", + " 98,\n", + " 41,\n", + " 67,\n", + " 5,\n", + " 73,\n", + " 21,\n", + " 64,\n", + " 63,\n", + " 43,\n", + " 54,\n", + " 90,\n", + " 20,\n", + " 25,\n", + " 59,\n", + " 78,\n", + " 93,\n", + " 47,\n", + " 12,\n", + " 87,\n", + " 40,\n", + " 31,\n", + " 83,\n", + " 10,\n", + " 89,\n", + " 68,\n", + " 88,\n", + " 8,\n", + " 60,\n", + " 15,\n", + " 56,\n", + " 45,\n", + " 77,\n", + " 46,\n", + " 62,\n", + " 2,\n", + " 38,\n", + " 74,\n", + " 27,\n", + " 48,\n", + " 1,\n", + " 32,\n", + " 4,\n", + " 91,\n", + " 86,\n", + " 16,\n", + " 42,\n", + " 76,\n", + " 26,\n", + " 37,\n", + " 11,\n", + " 99,\n", + " 95,\n", + " 100,\n", + " 36,\n", + " 17,\n", + " 24,\n", + " 61,\n", + " 35,\n", + " 75,\n", + " 52,\n", + " 7,\n", + " 57]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "sample_list_1 = random.sample(range(101),k=80)\n", + "sample_list_1" ] }, { @@ -54,11 +146,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The length of set1 is 80 and it is True that it is the same as the length of sample_list_1\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set1 = set(sample_list_1)\n", + "len_set = len(set1)\n", + "\n", + "print(f\"The length of set1 is {len_set} and it is {len_set == len(sample_list_1)} that it is the same as the length of sample_list_1\")" ] }, { @@ -81,7 +185,11 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "sample_list_2 = [random.randint(0,100) for i in range(80)]\n", + "\n", + "sample_list_2\n", + "\n" ] }, { @@ -93,11 +201,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "51\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set2 = set(sample_list_2)\n", + "print(len(set2)) #this is no longer 80 because sets ignore the duplicate elements in sample_list_2" ] }, { @@ -109,11 +227,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set3 = set1.difference(set2)\n" ] }, { @@ -125,11 +244,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{3, 9, 29, 39, 53, 55, 66, 71, 82, 92}" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set4 = set2.difference(set1)\n", + "set4\n" ] }, { @@ -141,11 +273,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set5 = set1.intersection(set2)\n", + "\n" ] }, { @@ -157,11 +291,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set6 = set()" ] }, { @@ -173,11 +308,103 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1,\n", + " 2,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 10,\n", + " 11,\n", + " 12,\n", + " 14,\n", + " 15,\n", + " 16,\n", + " 17,\n", + " 18,\n", + " 20,\n", + " 21,\n", + " 23,\n", + " 24,\n", + " 25,\n", + " 26,\n", + " 27,\n", + " 28,\n", + " 31,\n", + " 32,\n", + " 34,\n", + " 35,\n", + " 36,\n", + " 37,\n", + " 38,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 49,\n", + " 50,\n", + " 52,\n", + " 54,\n", + " 56,\n", + " 57,\n", + " 59,\n", + " 60,\n", + " 61,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 67,\n", + " 68,\n", + " 70,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 75,\n", + " 76,\n", + " 77,\n", + " 78,\n", + " 79,\n", + " 80,\n", + " 83,\n", + " 85,\n", + " 86,\n", + " 87,\n", + " 88,\n", + " 89,\n", + " 90,\n", + " 91,\n", + " 93,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97,\n", + " 98,\n", + " 99,\n", + " 100}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set6.update(set3,set5)\n", + "set6" ] }, { @@ -189,11 +416,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set1 == set6" ] }, { @@ -205,11 +444,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False True\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(set2.issubset(set1),set3.issubset(set1))" ] }, { @@ -223,11 +471,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set7 = set3.union(set4,set5)\n", + "set8 = set1.union(set2)\n", + "set7 == set8" ] }, { @@ -239,11 +501,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "#Since sets in Python are unordered collections, there is no concept of a \"first\" element in a set. \n", + "#Therefore, removing the \"first\" element from a set using the pop() method doesn't make sense in the traditional sense as it does for lists.\n", + "#I could use set1.pop() to remove an arbitrary element from the set, or convert set 1 into a list to temporarily remove the first element.\n", + "#I will use the option to covnert the set into a list\n", + "\n", + "list1 = list(set1)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list1.pop(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "set1 = set(list1)\n" ] }, { @@ -259,11 +556,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 4, 5, 6, 7, 8, 10, 12, 14, 15, 16, 17, 18, 20, 23, 24, 25, 26, 27, 28, 32, 34, 35, 36, 37, 38, 40, 42, 43, 44, 45, 46, 47, 48, 50, 52, 54, 56, 57, 60, 62, 63, 64, 65, 67, 68, 70, 72, 73, 74, 75, 76, 77, 78, 80, 83, 85, 86, 87, 88, 90, 93, 94, 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", + "\n", + "for element in list_to_remove:\n", + " if element in set1:\n", + " set1.remove(element)\n", + "\n", + "print(set1)" ] }, { @@ -278,7 +590,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ @@ -304,11 +616,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Father's set: {'watermelon', 'cucumber', 'gums', 'yogurt', 'onions', 'flour', 'garlic', 'toilet paper'}\n", + "Mother's set: set()\n", + "Jo's set: {'watermelon', 'cucumber', 'juice', 'sugar', 'milk', 'gums', 'yogurt', 'onions', 'blueberries', 'garlic', 'tomatoes'}\n", + "Carlos's set: {'cherries', 'blueberries', 'onions', 'flour', 'water', 'garlic', 'toilet paper', 'tomatoes'}\n", + "Mattia's set: set()\n" + ] + } + ], + "source": [ + "# 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)\n", + "\n", + "print(\"Father's set:\", father_set)\n", + "print(\"Mother's set:\", mother_set)\n", + "print(\"Jo's set:\", Jo_set)\n", + "print(\"Carlos's set:\", Carlos_set)\n", + "print(\"Mattia's set:\", Mattia_set)" ] }, { @@ -322,11 +657,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Mattia's set: {'watermelon', 'cucumber', 'juice', 'sugar', 'cherries', 'milk', 'gums', 'yogurt', 'flour', 'water', 'toilet paper'}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "Mattia_set = Carlos_set.symmetric_difference(Jo_set)\n", + "print(\"Updated Mattia's set:\", Mattia_set)" ] }, { @@ -338,9 +683,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['cucumber', 'milk', 'mushrooms', 'onions', 'bread', 'yogurt', 'mushrooms', 'chocolate', 'bread', 'onions']\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 +705,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'bread', 'chocolate', 'cucumber', 'milk', 'mushrooms', 'onions', 'yogurt'}" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "mother_set = {item for item in mother_list} #using another method here than above, for variation\n", + "mother_set" ] }, { @@ -373,11 +739,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "sets_list = [father_set, mother_set, Jo_set, Carlos_set, Mattia_set]\n", + "\n", + "for s in sets_list:\n", + " s.discard(\"toilet paper\")" ] }, { @@ -389,11 +759,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'watermelon', 'juice', 'sugar', 'cherries', 'gums', 'onions', 'flour', 'bread', 'blueberries', 'tomatoes', 'water', 'cucumber', 'mushrooms', 'milk', 'yogurt', 'chocolate', 'garlic'}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "groceries_set = mother_set.union(father_set, Jo_set, Carlos_set, Mattia_set)\n", + "print(groceries_set)" ] }, { @@ -407,17 +787,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Bought: watermelon\n", + "Bought: juice\n", + "Bought: sugar\n", + "Bought: cherries\n", + "Bought: gums\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "for i in range(5):\n", + " item_bought = groceries_set.pop()\n", + " print(\"Bought:\",item_bought)\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -431,9 +833,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..77a0b19 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": 8, "metadata": {}, "outputs": [], "source": [ @@ -49,11 +49,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "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 = list(word_freq.keys())\n", + "keys_list.sort()\n", + "word_freq2 = {}\n", + "for element in keys_list:\n", + " word_freq2[element] = word_freq[element]\n", + "\n", + "print(word_freq2)" ] }, { @@ -90,11 +105,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "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", + "import operator\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "print(sorted_tups)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "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" + "word_freq2 = {}\n", + "for key, value in sorted_tups:\n", + " word_freq2[key] = value\n", + "print(word_freq2)" ] }, { @@ -117,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -139,11 +185,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "0 love 25\n", + "1 conversation 1\n", + "2 every 6\n", + "3 we're 1\n", + "4 plate 1\n", + ".. ... ...\n", + "135 bedsheets 3\n", + "136 fill 2\n", + "137 hours 2\n", + "138 stop 1\n", + "139 bar 1\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df = pd.DataFrame(list(word_freq.items()), columns=['word', 'freq'])\n", + "print(df)\n", + "\n", + "# list(word_freq.items()) is used to prepare the data in a compatible format for the DataFrame constructor, ensuring that each key-value pair from the dictionary becomes a row in the DataFrame." ] }, { @@ -157,11 +228,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "120 a 8\n", + "109 about 1\n", + "43 all 1\n", + "96 although 3\n", + "72 and 23\n", + ".. ... ...\n", + "128 were 3\n", + "41 where 1\n", + "54 with 22\n", + "15 you 16\n", + "97 your 21\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by = 'word', ascending = True, inplace = True)\n", + "print(df)" ] }, { @@ -173,17 +267,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "114 let 1\n", + "87 trust 1\n", + "11 man 1\n", + "45 make 1\n", + "46 lover 1\n", + ".. ... ...\n", + "0 love 25\n", + "65 in 27\n", + "121 my 33\n", + "56 come 37\n", + "126 on 40\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by = 'freq', ascending = True, inplace = True)\n", + "print(df)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -197,9 +321,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 }