diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 2e59d77..37e662a 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup = (\"I\",)" ] }, { @@ -33,11 +34,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "type(tup)" ] }, { @@ -55,13 +68,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# Your code here\n", "\n", - "# Your explanation here\n" + "# Nocode\n", + "\n", + "# Your explanation here\n", + "\n", + "# You can't append elements to tuples since they are immutable" ] }, { @@ -79,13 +96,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ "# Your code here\n", "\n", - "# Your explanation here\n" + "tup = tup + (\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "print(tup)\n", + "\n", + "# Your explanation here\n", + "## Since tuples are immutable, we can create a new tuple with the previous elements plus the new elements we wish to add." ] }, { @@ -103,11 +132,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n')\n", + "('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup1 = tup[:4]\n", + "tup2 = tup[-4:]\n", + "\n", + "print(tup1)\n", + "print(tup2)" ] }, { @@ -121,11 +163,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "tup3 and tup are the same\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup3 = tup1 + tup2\n", + "print(tup3) # (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "\n", + "if tup3 == tup:\n", + " print(\"tup3 and tup are the same\")\n", + "else:\n", + " print(\"tup3 and tup are not the same\")" ] }, { @@ -137,11 +195,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tup1 has 4 elements, and tup2 has 4\n", + "The the result of the sum of tup1 and tup2 is the same as the total letters in tup 3\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "count1 = len(tup1)\n", + "count2 = len(tup2)\n", + "\n", + "print(\"tup1 has\",count1,\"elements, and tup2 has\",count2)\n", + "\n", + "if count1 + count2 == len(tup3):\n", + " print(\"The the result of the sum of tup1 and tup2 is the same as the total letters in tup 3\")\n", + "else:\n", + " print(\"The result of the sum of tup1 and tup2 is not the same as the total letters in tup 3\")" ] }, { @@ -153,11 +229,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(tup3.index(\"h\"))" ] }, { @@ -177,11 +262,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a is in tup3: True\n", + "b is in tup3: False\n", + "c is in tup3: True\n", + "d is in tup3: False\n", + "e is in tup3: False\n" + ] + } + ], "source": [ - "# Your code here\n" + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "for x in letters:\n", + " if x in tup3:\n", + " print(x, \"is in tup3:\", True)\n", + " else:\n", + " print(x, \"is in tup3:\", False)" ] }, { @@ -195,12 +297,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a appears 1 time(s) in tup3\n", + "b appears 0 time(s) in tup3\n", + "c appears 1 time(s) in tup3\n", + "d appears 0 time(s) in tup3\n", + "e appears 0 time(s) in tup3\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "for letter in letters:\n", + " count = tup3.count(letter)\n", + " print(letter, \"appears\", count, \"time(s) in tup3\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -219,7 +343,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.7" + }, + "vscode": { + "interpreter": { + "hash": "721db305ef1fd1fc91cdf20e400af694a949fe540ac5f48c160f31c7e384879d" + } } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index cb3a3e0..dd325a1 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": 47, "metadata": {}, "outputs": [], "source": [ @@ -38,11 +38,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[60, 63, 98, 71, 87, 11, 12, 10, 62, 15, 36, 67, 88, 42, 59, 83, 48, 46, 79, 57, 68, 97, 80, 35, 89, 70, 90, 44, 54, 78, 28, 32, 47, 6, 74, 49, 96, 76, 5, 13, 55, 37, 27, 93, 17, 4, 18, 22, 38, 14, 75, 9, 81, 94, 72, 99, 50, 58, 66, 26, 2, 34, 64, 20, 43, 19, 3, 91, 0, 61, 8, 30, 16, 29, 25, 52, 23, 40, 95, 73]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "sample_list_1 = random.sample(range(100),k=80)\n", + "print(sample_list_1)\n", + "\n", + "##random.sample already provides unique values according to documentations so not test for repeat values" ] }, { @@ -54,11 +66,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The length of set1 is 80\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set1 = set(sample_list_1)\n", + "print(\"The length of set1 is\", len(set1))" ] }, { @@ -77,11 +99,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[16, 79, 72, 36, 44, 2, 72, 6, 3, 70, 25, 95, 44, 96, 34, 78, 56, 30, 24, 95, 9, 36, 55, 80, 32, 74, 2, 40, 86, 68, 86, 69, 60, 46, 52, 78, 16, 99, 63, 84, 22, 44, 3, 90, 69, 86, 70, 29, 57, 93, 18, 90, 77, 95, 69, 63, 41, 12, 84, 28, 48, 32, 83, 17, 51, 28, 65, 36, 76, 75, 78, 96, 3, 45, 15, 60, 62, 49, 32, 17]\n" + ] + } + ], + "source": [ + "sample_list_2 = []\n", + "for i in range(80):\n", + " value = random.randint(0, 100)\n", + " sample_list_2.append(value)\n", + "\n", + "print(sample_list_2)\n" ] }, { @@ -93,11 +128,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The length of set2 is 80\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set2 = set(sample_list_2)\n", + "print(\"The length of set2 is\", len(set1))" ] }, { @@ -109,11 +154,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 4, 5, 8, 10, 11, 13, 14, 19, 20, 23, 26, 27, 35, 37, 38, 42, 43, 47, 50, 54, 58, 59, 61, 64, 66, 67, 71, 73, 81, 87, 88, 89, 91, 94, 97, 98}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set3 = set1 - set2\n", + "print(set3)\n", + "#If we substract the elements from set2 from set1 we will get the set3 elements" ] }, { @@ -125,11 +180,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{65, 69, 41, 45, 77, 51, 84, 86, 24, 56}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set4 = set2 - set1\n", + "print(set4)\n", + "#Same as above, but the other way around" ] }, { @@ -141,11 +206,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 6, 9, 12, 15, 16, 17, 18, 22, 25, 28, 29, 30, 32, 34, 36, 40, 44, 46, 48, 49, 52, 55, 57, 60, 62, 63, 68, 70, 72, 74, 75, 76, 78, 79, 80, 83, 90, 93, 95, 96, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set5 = set1 & set2\n", + "print(set5)\n", + "#We can use & to intersect both, avoiding repeated values" ] }, { @@ -157,11 +232,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "set6 = set()" ] }, { @@ -173,11 +248,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 25, 26, 27, 28, 29, 30, 32, 34, 35, 36, 37, 38, 40, 42, 43, 44, 46, 47, 48, 49, 50, 52, 54, 55, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, 70, 71, 72, 73, 74, 75, 76, 78, 79, 80, 81, 83, 87, 88, 89, 90, 91, 93, 94, 95, 96, 97, 98, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set6.update(set3)\n", + "set6.update(set5)\n", + "print(set6)" ] }, { @@ -189,11 +274,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Equal\n" + ] + } + ], "source": [ - "# Your code here\n" + "if set1 == set6:\n", + " print(\"Equal\")\n", + "else:\n", + " print(\"Not Equal\")\n" ] }, { @@ -205,11 +301,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Set1 does not contain Set2\n", + "Set1 contains Set3\n" + ] + } + ], + "source": [ + "if set2.issubset(set1):\n", + " print(\"Set1 contains Set2\")\n", + "else:\n", + " print(\"Set1 does not contain Set2\")\n", + "\n", + "if set3.issubset(set1):\n", + " print(\"Set1 contains Set3\")\n", + "else:\n", + " print(\"Set1 does not contain Set3\")" ] }, { @@ -223,11 +336,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "These two sets are equal\n" + ] + } + ], + "source": [ + "set3_4_5 = set3.union(set4).union(set5)\n", + "set1_2 = set1.union(set2)\n", + "\n", + "if set3_4_5 == set1_2:\n", + " print(\"These two sets are equal\")\n", + "else:\n", + " print(\"These two sets are not equal.\")\n" ] }, { @@ -239,11 +366,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "set1.pop()\n" ] }, { @@ -259,11 +397,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 4, 5, 6, 8, 10, 12, 13, 14, 15, 16, 17, 18, 20, 22, 23, 25, 26, 27, 28, 30, 32, 34, 35, 36, 37, 38, 40, 42, 43, 44, 46, 47, 48, 50, 52, 54, 55, 57, 58, 60, 62, 63, 64, 66, 67, 68, 70, 72, 73, 74, 75, 76, 78, 80, 83, 87, 88, 90, 93, 94, 95, 96, 97, 98}\n" + ] + } + ], "source": [ - "# Your code here\n" + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "\n", + "for item in list_to_remove:\n", + " if item in set1:\n", + " set1.remove(item)\n", + "\n", + "print(set1)\n" ] }, { @@ -278,7 +430,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": {}, "outputs": [], "source": [ @@ -304,11 +456,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'garlic', 'watermelon', 'toilet paper', 'onions', 'yogurt', 'flour', 'cucumber', 'gums'}\n", + "set()\n", + "{'sugar', 'garlic', 'tomatoes', 'watermelon', 'blueberries', 'juice', 'yogurt', 'onions', 'milk', 'cucumber', 'gums'}\n", + "{'garlic', 'cherries', 'tomatoes', 'toilet paper', 'blueberries', 'onions', 'flour', 'water'}\n", + "set()\n" + ] + } + ], + "source": [ + "father_set = set(father_list)\n", + "print(father_set)\n", + "\n", + "mother_set = set(mother_list)\n", + "print(mother_set)\n", + "\n", + "Jo_set = set(Jo_list)\n", + "print(Jo_set)\n", + "\n", + "Carlos_set = set(Carlos_list)\n", + "print(Carlos_set)\n", + "\n", + "Mattia_set = set(Mattia_list)\n", + "print(Mattia_set)" ] }, { @@ -322,11 +499,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'sugar', 'cherries', 'watermelon', 'toilet paper', 'juice', 'yogurt', 'flour', 'water', 'milk', 'cucumber', 'gums'}\n" + ] + } + ], "source": [ - "# Your code here\n" + "new_set = Carlos_set.symmetric_difference(Jo_set)\n", + "Mattia_set = new_set.difference(Carlos_set.intersection(Jo_set))\n", + "print(Mattia_set)" ] }, { @@ -338,9 +525,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['deodorant', 'yogurt', 'shampoo', 'milk', 'juice', 'juice', 'milk', 'mushrooms', 'yogurt', 'tomatoes']\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 +547,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tomatoes', 'juice', 'yogurt', 'milk', 'deodorant', 'shampoo', 'mushrooms'}\n" + ] + } + ], + "source": [ + "mother_set = set(mother_list)\n", + "\n", + "print(mother_set)" ] }, { @@ -373,11 +578,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Collect sets into a list\n", + "sets_list = [father_set, mother_set, Jo_set, Carlos_set, Mattia_set]" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'garlic', 'watermelon', 'onions', 'yogurt', 'flour', 'cucumber', 'gums'}\n", + "{'tomatoes', 'juice', 'yogurt', 'milk', 'deodorant', 'shampoo', 'mushrooms'}\n", + "{'sugar', 'garlic', 'tomatoes', 'watermelon', 'blueberries', 'juice', 'yogurt', 'onions', 'milk', 'cucumber', 'gums'}\n", + "{'garlic', 'cherries', 'tomatoes', 'blueberries', 'onions', 'flour', 'water'}\n", + "{'sugar', 'cherries', 'watermelon', 'juice', 'yogurt', 'flour', 'water', 'milk', 'cucumber', 'gums'}\n" + ] + } + ], + "source": [ + "# Remove 'toilet paper' from each set\n", + "for my_set in sets_list:\n", + " my_set.discard('toilet paper')\n", + "\n", + "# Print the updated sets\n", + "print(father_set)\n", + "print(mother_set)\n", + "print(Jo_set)\n", + "print(Carlos_set)\n", + "print(Mattia_set)" ] }, { @@ -389,11 +625,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tomatoes', 'deodorant', 'shampoo', 'sugar', 'garlic', 'cherries', 'watermelon', 'onions', 'juice', 'yogurt', 'blueberries', 'flour', 'water', 'milk', 'cucumber', 'gums', 'mushrooms'}\n" + ] + } + ], "source": [ - "# Your code here\n" + "groceries = father_set.union(mother_set, Jo_set, Carlos_set, Mattia_set)\n", + "print(groceries)" ] }, { @@ -405,14 +650,34 @@ "##### Hint: use the `.pop()` method." ] }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'sugar', 'garlic', 'tomatoes', 'deodorant', 'shampoo'}\n" + ] + } + ], + "source": [ + "five_items = set()\n", + "for i in range(5):\n", + " item = groceries.pop()\n", + " five_items.add(item)\n", + "\n", + "print(five_items)\n" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here\n" - ] + "source": [] } ], "metadata": { @@ -431,7 +696,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.10.7" + }, + "vscode": { + "interpreter": { + "hash": "721db305ef1fd1fc91cdf20e400af694a949fe540ac5f48c160f31c7e384879d" + } } }, "nbformat": 4, diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 7ab8ea5..95f0b40 100644 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -49,11 +49,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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 = list(word_freq.keys())\n", + "keys.sort()\n", + "word_freq2 = {}\n", + "for key in keys:\n", + " word_freq2[key] = word_freq[key]\n", + "print(word_freq2)\n" ] }, { @@ -90,11 +103,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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", + "\n", + "sorted_word_freq = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "\n", + "word_freq2 = {}\n", + "for k, v in sorted_word_freq:\n", + " word_freq2[k] = v\n", + "\n", + "print(word_freq2)\n" ] }, { @@ -110,7 +139,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -132,11 +161,81 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordfreq
0love25
1conversation1
2every6
3we're1
4plate1
\n", + "
" + ], + "text/plain": [ + " word freq\n", + "0 love 25\n", + "1 conversation 1\n", + "2 every 6\n", + "3 we're 1\n", + "4 plate 1" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "df = pd.DataFrame(list(word_freq.items()), columns=['word', 'freq'])\n", + "df.head()" ] }, { @@ -150,11 +249,81 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordfreq
120a8
109about1
43all1
96although3
72and23
\n", + "
" + ], + "text/plain": [ + " word freq\n", + "120 a 8\n", + "109 about 1\n", + "43 all 1\n", + "96 although 3\n", + "72 and 23" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "df = df.sort_values('word', ascending=True)\n", + "df.head()" ] }, { @@ -166,12 +335,89 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordfreq
114let1
87trust1
11man1
45make1
46lover1
\n", + "
" + ], + "text/plain": [ + " word freq\n", + "114 let 1\n", + "87 trust 1\n", + "11 man 1\n", + "45 make 1\n", + "46 lover 1" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "df = df.sort_values('freq', ascending=True)\n", + "df.head()\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -190,7 +436,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.7" + }, + "vscode": { + "interpreter": { + "hash": "721db305ef1fd1fc91cdf20e400af694a949fe540ac5f48c160f31c7e384879d" + } } }, "nbformat": 4,