diff --git a/your-code/challenges.ipynb b/your-code/challenges.ipynb index ba91b3f..fa6b803 100644 --- a/your-code/challenges.ipynb +++ b/your-code/challenges.ipynb @@ -13,11 +13,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "tup = (\"I\",)\n" ] }, { @@ -31,11 +31,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ - "# Your code here" + "print(type(tup))" ] }, { @@ -59,10 +67,9 @@ }, "outputs": [], "source": [ - "# Your code here\n", + "tup.append(\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", "\n", - "# Your explanation here\n", - "# You can :) " + "# Tuples are immutable. You cannot add, remove, or change elements after the tuple is created, it will retun an errror \"AttributeError: 'tuple' object has no attribute 'append'\".\n" ] }, { @@ -80,11 +87,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here" + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "print(tup)\n" ] }, { @@ -102,11 +118,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n')\n", + "('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here" + "tup1 = tup[0:4]\n", + "print(tup1)\n", + "\n", + "tup2 = tup[-4:] \n", + "print(tup2)" ] }, { @@ -120,11 +149,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here" + "tup3 = tup1 + tup2\n", + "print(tup3)\n" ] }, { @@ -136,11 +174,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "4\n", + "8\n" + ] + } + ], "source": [ - "# Your code here" + "count1 = len(tup1)\n", + "print(count1)\n", + "count2 = len(tup2)\n", + "print(count2)\n", + "total = count1 + count2\n", + "print(total) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "The total count matches tup3.\n" + ] + } + ], + "source": [ + "count3 = len(tup3)\n", + "print(count3)\n", + "\n", + "if total == count3:\n", + " print(\"The total count matches tup3.\")\n", + "else:\n", + " print(\"The total count does not match tup3.\")" ] }, { @@ -152,11 +229,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ - "# Your code here" + "tup3.index(\"h\")\n", + "print(tup3.index(\"h\"))" ] }, { @@ -176,20 +262,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "tup3 = ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "False\n", + "False\n" + ] + } + ], + "source": [ + "for letter in letters:\n", + " if letter in tup3:\n", + " print(True)\n", + " else:\n", + " print(False)" ] }, { @@ -203,11 +306,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The letter 'a' appears 1 times in tup3.\n", + "The letter 'b' appears 0 times in tup3.\n", + "The letter 'c' appears 1 times in tup3.\n", + "The letter 'd' appears 0 times in tup3.\n", + "The letter 'e' appears 0 times in tup3.\n" + ] + } + ], "source": [ - "# Your code here" + "for letter in letters:\n", + " count = tup3.count(letter)\n", + " print(f\"The letter '{letter}' appears {count} times in tup3.\")\n", + " " ] }, { @@ -223,7 +341,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ @@ -248,11 +366,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[25, 56, 77, 55, 51, 2, 84, 86, 19, 26, 40, 94, 36, 81, 3, 7, 69, 87, 98, 6, 35, 8, 24, 5, 13, 29, 59, 65, 52, 21, 45, 61, 89, 66, 50, 11, 85, 75, 48, 57, 30, 18, 78, 100, 68, 37, 96, 67, 95, 73, 44, 28, 91, 54, 47, 1, 74, 64, 0, 31, 79, 83, 99, 43, 46, 27, 80, 14, 93, 70, 88, 92, 39, 20, 72, 53, 15, 58, 71, 41]\n" + ] + } + ], "source": [ - "# Your code here" + "sample_list_1 = random.sample(range(0, 101), 80)\n", + "print(sample_list_1)" ] }, { @@ -264,11 +391,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of set1: 80\n" + ] + } + ], "source": [ - "# your code here" + "set1 = set(sample_list_1)\n", + "\n", + "print(\"Length of set1:\", len(set1))\n", + "# yes, it still 80 because the sample function generates a list of unique numbers, so there are no duplicates to remove when converting to a set.\n" ] }, { @@ -287,11 +425,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[93, 58, 89, 11, 68, 40, 56, 13, 42, 7, 78, 47, 43, 64, 35, 24, 48, 6, 53, 77, 33, 35, 21, 78, 37, 25, 26, 92, 42, 96, 87, 66, 84, 26, 38, 37, 96, 52, 54, 10, 51, 43, 1, 38, 47, 16, 79, 68, 67, 54, 79, 1, 15, 41, 95, 44, 17, 36, 53, 43, 55, 6, 99, 31, 13, 66, 35, 80, 14, 11, 89, 40, 18, 80, 26, 94, 16, 36, 17, 72]\n" + ] + } + ], "source": [ - "# your code here" + "import random\n", + "\n", + "sample_list_2 = []\n", + "for _ in range(80):\n", + " sample_list_2.append(random.randint(0, 100))\n", + "print(sample_list_2)" ] }, { @@ -303,11 +454,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of set2: 53\n" + ] + } + ], "source": [ - "# Your code here" + "set2 = set(sample_list_2)\n", + "\n", + "print(\"Length of set2:\", len(set2))\n", + "# No, because random.randint can generate duplicate numbers, so when converting to a set, those duplicates are removed, resulting in a set that may have fewer than 80 unique elements." ] }, { @@ -319,11 +481,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Elements in set1 but not in set2: {0, 2, 3, 5, 8, 19, 20, 27, 28, 29, 30, 39, 45, 46, 50, 57, 59, 61, 65, 69, 70, 71, 73, 74, 75, 81, 83, 85, 86, 88, 91, 98, 100}\n" + ] + } + ], "source": [ - "# Your code here" + "set3 = set1.difference(set2)\n", + "print(\"Elements in set1 but not in set2:\", set3)" ] }, { @@ -335,11 +506,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Elements in set2 but not in set1: {33, 38, 10, 42, 16, 17}\n" + ] + } + ], "source": [ - "# Your code here" + "set4 = set2.difference(set1)\n", + "print(\"Elements in set2 but not in set1:\", set4)" ] }, { @@ -351,11 +531,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Elements common to both set1 and set2: {1, 6, 7, 11, 13, 14, 15, 18, 21, 24, 25, 26, 31, 35, 36, 37, 40, 41, 43, 44, 47, 48, 51, 52, 53, 54, 55, 56, 58, 64, 66, 67, 68, 72, 77, 78, 79, 80, 84, 87, 89, 92, 93, 94, 95, 96, 99}\n" + ] + } + ], "source": [ - "# Your code here" + "set5 = set1.intersection(set2)\n", + "print(\"Elements common to both set1 and set2:\", set5)" ] }, { @@ -375,11 +564,39 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "len(set1) = 80\n", + "len(set2) = 53\n", + "len(set3) = 33\n", + "len(set4) = 6\n", + "len(set5) = 47\n", + "len(set1) == len(set3) + len(set5): True\n", + "len(set2) == len(set4) + len(set5): True\n", + "len(set1 | set2) == len(set3) + len(set4) + len(set5): True\n" + ] + } + ], + "source": [ + "print(\"len(set1) =\", len(set1))\n", + "print(\"len(set2) =\", len(set2))\n", + "print(\"len(set3) =\", len(set3))\n", + "print(\"len(set4) =\", len(set4))\n", + "print(\"len(set5) =\", len(set5))\n", + "\n", + "# Check if set1 = set3 + set5\n", + "print(\"len(set1) == len(set3) + len(set5):\", len(set1) == len(set3) + len(set5))\n", + "\n", + "# Check if set2 = set4 + set5\n", + "print(\"len(set2) == len(set4) + len(set5):\", len(set2) == len(set4) + len(set5))\n", + "\n", + "# Check union formula\n", + "print(\"len(set1 | set2) == len(set3) + len(set4) + len(set5):\", len(set1 | set2) == len(set3) + len(set4) + len(set5))" ] }, { @@ -391,11 +608,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "set6 = set()" ] }, { @@ -407,11 +624,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 2, 3, 5, 6, 7, 8, 11, 13, 14, 15, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 30, 31, 35, 36, 37, 39, 40, 41, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 98, 99, 100}\n" + ] + } + ], "source": [ - "# Your code here" + "set6.update(set3, set5)\n", + "print(set6)" ] }, { @@ -427,7 +653,10 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "if set1 == set6:\n", + " print(\"set1 is equal to set6\")\n", + "else:\n", + " print(\"set1 is not equal to set6\")" ] }, { @@ -439,11 +668,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], "source": [ - "# Your code here" + "print(set2.issubset(set1))\n", + "print(set3.issubset(set1))" ] }, { @@ -457,11 +696,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Aggregation of set3, set4, set5: {0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 30, 31, 33, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 98, 99, 100}\n", + "Aggregation of set1 and set2: {0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 30, 31, 33, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 98, 99, 100}\n", + "Are the aggregated sets equal? True\n" + ] + } + ], "source": [ - "# Your code here" + "# Aggregate (union) set3, set4, and set5\n", + "agg1 = set3.union(set4, set5)\n", + "print(\"Aggregation of set3, set4, set5:\", agg1)\n", + "\n", + "# Aggregate (union) set1 and set2\n", + "agg2 = set1.union(set2)\n", + "print(\"Aggregation of set1 and set2:\", agg2)\n", + "\n", + "# Check if the aggregated values are equal\n", + "print(\"Are the aggregated sets equal?\", agg1 == agg2)\n" ] }, { @@ -473,11 +731,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Removed element: 0\n", + "Updated set1: {1, 2, 3, 5, 6, 7, 8, 11, 13, 14, 15, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 30, 31, 35, 36, 37, 39, 40, 41, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 98, 99, 100}\n" + ] + } + ], "source": [ - "# Your code here" + "removed_element = set1.pop()\n", + "\n", + "print(\"Removed element:\", removed_element)\n", + "print(\"Updated set1:\", set1)" ] }, { @@ -493,11 +763,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Remaining elements in set1: {2, 3, 5, 6, 7, 8, 13, 14, 15, 18, 20, 24, 25, 26, 27, 28, 30, 35, 36, 37, 40, 43, 44, 45, 46, 47, 48, 50, 52, 53, 54, 55, 56, 57, 58, 64, 65, 66, 67, 68, 70, 72, 73, 74, 75, 77, 78, 80, 83, 84, 85, 86, 87, 88, 92, 93, 94, 95, 96, 98, 100}\n" + ] + } + ], "source": [ - "# Your code here" + "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", + "# Convert list to a set for efficiency\n", + "remove_set = set(list_to_remove)\n", + "\n", + "# Remove elements that are present in both sets\n", + "set1.difference_update(remove_set)\n", + "print(\"Remaining elements in set1:\", set1)\n" ] }, { @@ -515,7 +800,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": {}, "outputs": [], "source": [ @@ -551,11 +836,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "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" + "# Step 1: Extract keys and convert to a list\n", + "keys = list(word_freq.keys())\n", + "\n", + "# Step 2: Sort the keys list\n", + "keys.sort()\n", + "\n", + "# Step 3: Create an empty dictionary\n", + "word_freq2 = {}\n", + "\n", + "# Step 4: Loop through sorted keys and populate word_freq2\n", + "for key in keys:\n", + " word_freq2[key] = word_freq[key]\n", + "\n", + "# Step 5: Print the new dictionary\n", + "print(word_freq2)\n" ] }, { @@ -592,17 +899,40 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 66, + "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" + "import operator\n", + "\n", + "word_freq = {'love': 25, 'conversation': 1, 'every': 6, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'now': 11, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'push': 3, 'baby': 14, 'going': 1, 'you': 16, \"don't\": 2, 'one': 1, 'mind': 2, 'backseat': 1, 'friends': 1, 'then': 3, 'know': 2, 'take': 1, 'play': 1, 'okay': 1, 'so': 2, 'begin': 1, 'start': 2, 'over': 1, 'body': 17, 'boy': 2, 'just': 1, 'we': 7, 'are': 1, 'girl': 2, 'tell': 1, 'singing': 2, 'drinking': 1, 'put': 3, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, \"i'm\": 23, 'like': 10, 'can': 1, 'doing': 2, 'with': 22, 'club': 1, 'come': 37, 'it': 1, 'somebody': 2, 'handmade': 2, 'out': 1, 'new': 6, 'room': 3, 'chance': 1, 'follow': 6, 'in': 27, 'may': 2, 'brand': 6, 'that': 2, 'magnet': 3, 'up': 3, 'first': 1, 'and': 23, 'pull': 3, 'of': 6, 'table': 1, 'much': 2, 'last': 3, 'i': 6, 'thrifty': 1, 'grab': 2, 'was': 2, 'driver': 1, 'slow': 1, 'dance': 1, 'the': 18, 'say': 2, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'me': 10, 'do': 3, 'waist': 2, 'smell': 3, 'day': 6, 'although': 3, 'your': 21, 'leave': 1, 'want': 2, \"let's\": 2, 'lead': 6, 'at': 1, 'hand': 1, 'how': 1, 'talk': 4, 'not': 2, 'eat': 1, 'falling': 3, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'crazy': 2, 'let': 1, 'too': 5, 'van': 1, 'shots': 1, 'go': 2, 'to': 2, 'a': 8, 'my': 33, 'is': 5, 'place': 1, 'find': 1, 'shape': 6, 'on': 40, 'kiss': 1, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'discovering': 6, 'something': 6, 'be': 16, 'bedsheets': 3, 'fill': 2, 'hours': 2, 'stop': 1, 'bar': 1}\n", + "\n", + "# Step 1: Sort dictionary items by value (ascending)\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "\n", + "# Step 2: Create a new empty dictionary\n", + "word_freq2 = {}\n", + "\n", + "# Step 3: Insert each key-value pair from the sorted list into the new dictionary\n", + "for key, value in sorted_tups:\n", + " word_freq2[key] = value\n", + "\n", + "# Step 4: Print the new dictionary (values sorted ascendingly)\n", + "print(word_freq2)\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -616,12 +946,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } + "version": "3.13.7" } }, "nbformat": 4,