diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..1ab94ac Binary files /dev/null and b/.DS_Store differ diff --git a/your-code/.DS_Store b/your-code/.DS_Store new file mode 100644 index 0000000..4f58acd Binary files /dev/null and b/your-code/.DS_Store differ diff --git a/your-code/challenges.ipynb b/your-code/challenges.ipynb index ba91b3f..5c79e28 100644 --- a/your-code/challenges.ipynb +++ b/your-code/challenges.ipynb @@ -13,11 +13,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "tup = (\"I\")" ] }, { @@ -31,11 +32,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "print(tup)" ] }, { @@ -53,15 +63,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "can only concatenate str (not \"tuple\") to str", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/jp/zwtw3mp955j05l84q6rnw_4h0000gn/T/ipykernel_36878/3709429359.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Your code here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtup\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtup\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"r\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"o\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"h\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"a\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"c\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"k\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mtup\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# Your explanation here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# You can`t append to a tuple in Python - Tuples are immutable: once you create them, you cannot add, remove, or change elements.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"tuple\") to str" + ] + } + ], "source": [ "# Your code here\n", - "\n", + "tup = tup + (\"r\",\"o\",\"h\",\"a\",\"c\",\"k\",)\n", + "print (tup)\n", "# Your explanation here\n", + "# You can`t append to a tuple in Python - Tuples are immutable: once you create them, you cannot add, remove, or change elements.\n", "# You can :) " ] }, @@ -82,9 +106,23 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "tup = (\"I\",)\n", + "#Here we didn’t “append” or change the first tuple; we simply created a new tuple and made the variable tup refer to it.\n", + "tup = (\"I\", \"r\",\"o\", \"n\", \"h\", \"a\", \"c\", \"k\") # reassing the variable\n", + "print (tup)\n", + "#Tuples are immutable → you can’t change the contents of an existing tuple.\n", + "#Variables are mutable names → you can make the name tup point to a different tuple.\n" ] }, { @@ -102,11 +140,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "('I', 'r', 'o', 'n')\n", + "('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "print (tup)\n", + "#Positive indices for the first 4 elements\n", + "tup1 = tup [0:4]\n", + "#Negative indices for the last 4 elementes\n", + "tup2 = tup[-4:]\n", + "\n", + "print(tup1)\n", + "print(tup2)" ] }, { @@ -120,11 +176,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "tup3 = tup1 + tup2\n", + "\n", + "print(tup3) \n", + "print(tup3 == tup)" ] }, { @@ -136,11 +205,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Count tup1: 4\n", + "Count tup2: 4\n", + "Total count: 8\n", + "Length of tup3: 8\n", + "True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "count1 = len(tup1)\n", + "count2 = len(tup2)\n", + "total = count1 + count2\n", + "\n", + "print(\"Count tup1:\", count1)\n", + "print(\"Count tup2:\", count2)\n", + "print(\"Total count:\", total)\n", + "print(\"Length of tup3:\", len(tup3))\n", + "print(total == len(tup3)) # check if equal" ] }, { @@ -152,11 +242,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "print(tup3.index(\"h\"))" ] }, { @@ -176,20 +275,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "False\n", + "False\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "for letter in letters : \n", + " print (letter in tup3)" ] }, { @@ -203,11 +317,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a - 1\n", + "b - 0\n", + "c - 1\n", + "d - 0\n", + "e - 0\n" + ] + } + ], "source": [ - "# Your code here" + "# 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", + " print(letter, \" - \", tup3.count(letter))\n" ] }, { @@ -223,7 +354,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ @@ -250,9 +381,22 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[61, 20, 26, 19, 66, 57, 74, 55, 84, 65, 89, 28, 87, 2, 99, 97, 73, 58, 98, 56, 94, 43, 75, 24, 30, 48, 93, 25, 16, 1, 23, 14, 35, 59, 47, 12, 39, 5, 44, 32, 10, 78, 4, 100, 36, 95, 90, 71, 53, 31, 69, 63, 27, 18, 64, 21, 6, 46, 50, 11, 72, 52, 33, 49, 54, 85, 68, 45, 0, 37, 96, 82, 60, 41, 42, 7, 38, 88, 29, 8]\n", + "Number of elements: 80\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "# List of 80 unique random integers from 0 to 100\n", + "sample_list_1 = random.sample(range(0, 101), 80)\n", + "print(sample_list_1)\n", + "print(\"Number of elements:\", len(sample_list_1))" ] }, { @@ -264,11 +408,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of set1: 80\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "# Convert list to set\n", + "set1 = set(sample_list_1)\n", + "\n", + "# Print the set length\n", + "print(\"Length of set1:\", len(set1))\n" ] }, { @@ -287,11 +444,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[38, 63, 74, 6, 59, 58, 9, 8, 83, 16, 35, 95, 88, 83, 73, 39, 20, 77, 6, 63, 13, 77, 4, 29, 79, 19, 83, 56, 86, 22, 40, 14, 66, 11, 20, 40, 27, 51, 15, 99, 43, 3, 0, 30, 40, 82, 82, 92, 73, 22, 48, 15, 23, 33, 10, 21, 68, 86, 73, 78, 21, 61, 80, 9, 88, 66, 91, 46, 41, 92, 89, 89, 85, 68, 94, 83, 61, 84, 67, 91]\n", + "Number of elements: 80\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "sample_list_2 = []\n", + "\n", + "for _ in range(80):\n", + " sample_list_2.append(random.randint(0, 100))\n", + "\n", + "print(sample_list_2)\n", + "print(\"Number of elements:\", len(sample_list_2))" ] }, { @@ -303,11 +476,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Lenght of set2: 56\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "set2 = set (sample_list_2)\n", + "print (\"Lenght of set2:\", len(set2))" ] }, { @@ -319,11 +502,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set3: {1, 2, 5, 7, 12, 18, 24, 25, 26, 28, 31, 32, 36, 37, 42, 44, 45, 47, 49, 50, 52, 53, 54, 55, 57, 60, 64, 65, 69, 71, 72, 75, 87, 90, 93, 96, 97, 98, 100}\n", + "Number of elements in set3: 39\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "set3 = set1 - set2\n", + "# or: set3 = set1.difference(set2)\n", + "\n", + "print(\"set3:\", set3)\n", + "print(\"Number of elements in set3:\", len(set3))\n" ] }, { @@ -335,11 +532,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set4: {3, 67, 40, 9, 13, 77, 15, 79, 80, 51, 83, 22, 86, 91, 92}\n", + "Number of elements in set4: 15\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "set4 = set2 - set1\n", + "# or: set4 = set2.difference(set1)\n", + "\n", + "print(\"set4:\", set4)\n", + "print(\"Number of elements in set4:\", len(set4))\n" ] }, { @@ -351,11 +562,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set5: {0, 4, 6, 8, 10, 11, 14, 16, 19, 20, 21, 23, 27, 29, 30, 33, 35, 38, 39, 41, 43, 46, 48, 56, 58, 59, 61, 63, 66, 68, 73, 74, 78, 82, 84, 85, 88, 89, 94, 95, 99}\n", + "Number of elements in set5: 41\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "set5 = set1 & set2\n", + "# or: set5 = set1.intersection(set2)\n", + "\n", + "print(\"set5:\", set5)\n", + "print(\"Number of elements in set5:\", len(set5))\n" ] }, { @@ -375,11 +600,41 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "#Relationship\n", + " # Let: \n", + " # |A| = len(set1)\n", + " # |B| = len(set2)\n", + " # |A \\ B| = len(set3)\n", + " # |B \\ A| = len(set4)\n", + " # |A ∩ B| = len(set5)\n", + "\n", + "#Then: \n", + " #|A| = |A ∖ B|+|A ∩ B|\n", + " #|B| = |B ∖ A| + |A ∩ B|\n", + "\n", + "#The size of the union is:\n", + " # |A ∪ B| =|A|+|B| − |A ∩ B|=|A ∖ B| + |B ∖ A| +|A ∩ B|\n", + "\n", + "# verify the formulas\n", + "print(len(set1) == len(set3) + len(set5)) # |A| = |A\\B| + |A∩B|\n", + "print(len(set2) == len(set4) + len(set5)) # |B| = |B\\A| + |A∩B|\n", + "print(len(set1 | set2) == len(set3) + len(set4) + len(set5)) # union check\n", + "\n" ] }, { @@ -391,11 +646,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n", + "\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "set6 = set()\n", + "\n", + "print(set6)\n", + "print(type(set6))\n" ] }, { @@ -407,11 +675,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 2, 4, 5, 6, 7, 8, 10, 11, 12, 14, 16, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 63, 64, 65, 66, 68, 69, 71, 72, 73, 74, 75, 78, 82, 84, 85, 87, 88, 89, 90, 93, 94, 95, 96, 97, 98, 99, 100}\n", + "Length of set6: 80\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "set6.update(set3, set5)\n", + "\n", + "print(set6)\n", + "print(\"Length of set6:\", len(set6))\n" ] }, { @@ -423,11 +704,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "print(set1 == set6)\n" ] }, { @@ -439,11 +729,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "# Check if set2 is a subset of set1\n", + "print(set2.issubset(set1)) \n", + "\n", + "# Check if set3 is a subset of set1\n", + "print(set3.issubset(set1)) \n" ] }, { @@ -457,11 +761,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "# Aggregate set3, set4, set5\n", + "agg1 = set3.union(set4, set5)\n", + "\n", + "# Aggregate set1 and set2\n", + "agg2 = set1.union(set2)\n", + "\n", + "# Compare\n", + "print(agg1 == agg2)\n" ] }, { @@ -473,11 +793,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "removed_element = set1.pop()\n" ] }, { @@ -493,11 +814,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Remaining elements in set1: {2, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20, 23, 24, 25, 26, 27, 28, 30, 32, 33, 35, 36, 37, 38, 42, 43, 44, 45, 46, 47, 48, 50, 52, 53, 54, 55, 56, 57, 58, 60, 63, 64, 65, 66, 68, 72, 73, 74, 75, 78, 82, 84, 85, 87, 88, 90, 93, 94, 95, 96, 97, 98, 100}\n" + ] + } + ], "source": [ - "# Your code here" + "# 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", + "# Remove all items in list_to_remove from set1 (if present)\n", + "set1.difference_update(list_to_remove)\n", + "\n", + "print(\"Remaining elements in set1:\", set1)\n", + "\n", + "for item in list_to_remove:\n", + " set1.discard(item) # discard removes if present, ignores if not\n" ] }, { @@ -515,7 +853,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": {}, "outputs": [], "source": [ @@ -551,11 +889,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 63, + "metadata": {}, + "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" + "# Your code here\n", + "\n", + "#Extract keys and convert to a list\n", + "keys = list(word_freq.keys())\n", + "\n", + "#Sort the keys alphabetically\n", + "keys.sort()\n", + "\n", + "#Create an empty dictionary\n", + "word_freq2 = {}\n", + "\n", + "#Loop through sorted keys and fill the new dictionary\n", + "for key in keys:\n", + " word_freq2[key] = word_freq[key]\n", + "\n", + "# Print to check\n", + "print(word_freq2)\n" ] }, { @@ -592,17 +954,40 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 64, + "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" + "# Your code here\n", + "import operator\n", + "\n", + "# Sort the items of word_freq by value (ascending)\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "\n", + "\n", + "#Create an empty dictionary\n", + "word_freq2 = {}\n", + "\n", + "#Fill the new dictionary with the sorted items\n", + "for k, v in sorted_tups:\n", + " word_freq2[k] = v\n", + "\n", + "#Print to verify\n", + "print(word_freq2)\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -616,12 +1001,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } + "version": "3.9.7" } }, "nbformat": 4,