From b89c322fd449773ade8637e17e000a053b2b4e2d Mon Sep 17 00:00:00 2001 From: aaghafari-dev Date: Fri, 7 Nov 2025 08:32:14 +0100 Subject: [PATCH] my homework_lan-dict-set-tuple --- your-code/challenges.ipynb | 531 +++++++++++++++++++++++++++++-------- 1 file changed, 423 insertions(+), 108 deletions(-) diff --git a/your-code/challenges.ipynb b/your-code/challenges.ipynb index ba91b3f..bec084b 100644 --- a/your-code/challenges.ipynb +++ b/your-code/challenges.ipynb @@ -13,11 +13,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "('I',)\n" + ] + } + ], "source": [ - "# Your code here" + "tup = tuple(\"I\")\n", + "print(type(tup)) \n", + "print(tup) " ] }, { @@ -31,11 +42,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ - "# Your code here" + "print(type(tup))" ] }, { @@ -57,12 +76,24 @@ "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'tuple' object has no attribute 'append'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[8]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mtup\u001b[49m\u001b[43m.\u001b[49m\u001b[43mappend\u001b[49m(\u001b[33m\"\u001b[39m\u001b[33mr\u001b[39m\u001b[33m\"\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mo\u001b[39m\u001b[33m\"\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mn\u001b[39m\u001b[33m\"\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mh\u001b[39m\u001b[33m\"\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33ma\u001b[39m\u001b[33m\"\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mc\u001b[39m\u001b[33m\"\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mk\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 2\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mtup after append: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtup\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n\u001b[32m 4\u001b[39m \u001b[38;5;66;03m# Your explanation here\u001b[39;00m\n\u001b[32m 5\u001b[39m \u001b[38;5;66;03m# You can :) \u001b[39;00m\n", + "\u001b[31mAttributeError\u001b[39m: 'tuple' object has no attribute 'append'" + ] + } + ], "source": [ - "# Your code here\n", + "tup.append(\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "print(f\"tup after append: {tup}\")\n", "\n", - "# Your explanation here\n", - "# You can :) " + "# we can't append to a tuple since it's immutable\n" ] }, { @@ -82,9 +113,38 @@ "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": [ + "tup = tuple([\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"])\n", + "print(tup)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here" + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "print(type(tup)) \n", + "print(tup)" ] }, { @@ -102,11 +162,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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", + "tup2 = tup[-4:]\n", + "print(tup2)\n" ] }, { @@ -120,11 +192,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here" + "tup3 = tup1 + tup2\n", + "print(tup3)" ] }, { @@ -136,11 +217,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " number of elements in tup1 and tup2: 8\n", + " number of elements in tup3: 8\n" + ] + } + ], + "source": [ + "tup1 = tup[0:4]\n", + "tup2 = tup[-4:]\n", + "tup3 = tup1 + tup2\n", + "num1 = len(tup1)\n", + "num2 = len(tup2)\n", + "num3 = len(tup3)\n", + "print(f\" number of elements in tup1 and tup2: {num1 + num2}\")\n", + "print(f\" number of elements in tup3: {num3}\")\n", + "\n" ] }, { @@ -152,11 +250,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "in tup3, the index of 'h' is: 4\n" + ] + } + ], "source": [ - "# Your code here" + "print(f\"in tup3, the index of 'h' is: {tup3.index('h')}\")" ] }, { @@ -176,20 +282,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True: letter a found at index 0 in tuple3\n", + "False: letter b not found in tuple3\n", + "True: letter c found at index 2 in tuple3\n", + "False: letter d not found in tuple3\n", + "False: letter e not found in tuple3\n" + ] + } + ], + "source": [ + "letters = (\"a\", \"b\", \"c\", \"d\", \"e\")\n", + "for i in range(len(letters)):\n", + " if letters[i] in tup3:\n", + " print(f\"True: letter {letters[i]} found at index {i} in tuple3\")\n", + " else:\n", + " print(f\"False: letter {letters[i]} not found in tuple3\")" ] }, { @@ -203,11 +317,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "number of occurrences of a in tuple3: 1\n", + "number of occurrences of c in tuple3: 1\n" + ] + } + ], "source": [ - "# Your code here" + "letters = (\"a\", \"b\", \"c\", \"d\", \"e\")\n", + "count = 0\n", + "for i in range(len(letters)):\n", + " if letters[i] in tup3:\n", + " count += 1\n", + " print(f\"number of occurrences of {letters[i]} in tuple3: {tup3.count(letters[i])}\")\n", + " " ] }, { @@ -223,7 +352,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -248,11 +377,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sample_list_1: [10, 28, 8, 67, 69, 30, 99, 11, 71, 47, 6, 90, 46, 94, 78, 76, 35, 93, 54, 41, 5, 36, 73, 57, 58, 13, 45, 1, 33, 53, 22, 75, 52, 38, 39, 87, 25, 68, 24, 26, 23, 7, 15, 49, 77, 19, 61, 81, 21, 82, 2, 16, 84, 27, 89, 42, 66, 95, 92, 43, 85, 70, 48, 4, 20, 18, 44, 32, 64, 79, 56, 86, 74, 3, 96, 97, 17, 37, 98, 88]\n" + ] + } + ], "source": [ - "# Your code here" + "sample_list_1 = random.sample(range(0, 100), k=80)\n", + "print(f\"sample_list_1: {sample_list_1}\")" ] }, { @@ -264,11 +402,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set1.length = 80\n" + ] + } + ], "source": [ - "# your code here" + "set1 = set(sample_list_1)\n", + "print(f\"set1.length = {len(set1)}\")" ] }, { @@ -287,11 +434,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sample_list_2: [34, 33, 22, 97, 46, 8, 30, 95, 62, 31, 2, 41, 27, 43, 79, 7, 62, 75, 64, 37, 75, 10, 55, 15, 46, 10, 94, 97, 57, 48, 92, 30, 41, 82, 87, 0, 57, 49, 33, 35, 28, 74, 96, 89, 71, 85, 63, 2, 73, 4, 55, 74, 96, 17, 72, 51, 7, 83, 20, 97, 41, 46, 87, 6, 14, 57, 65, 88, 92, 41, 75, 65, 44, 82, 35, 84, 96, 98, 97, 21]\n" + ] + } + ], "source": [ - "# your code here" + "sample_list_2 = []\n", + "for _ in range(80):\n", + " sample_list_2.append(random.choice(range(0, 100)))\n", + "print(f\"sample_list_2: {sample_list_2}\")" ] }, { @@ -303,11 +461,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "lent of sample2,80\n", + "set2.length = 53\n" + ] + } + ], "source": [ - "# Your code here" + "print(f\"lent of sample2,{len(sample_list_2)}\")\n", + "set2 = set(sample_list_2)\n", + "print(f\"set2.length = {len(set2)}\")" ] }, { @@ -319,11 +488,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the element are not in set2): {1, 3, 5, 11, 13, 16, 18, 19, 23, 24, 25, 26, 32, 36, 38, 39, 42, 45, 47, 52, 53, 54, 56, 58, 61, 66, 67, 68, 69, 70, 76, 77, 78, 81, 86, 90, 93, 99}\n" + ] + } + ], "source": [ - "# Your code here" + "set3 = set1 - set2\n", + "print(f\"the element are not in set2): {set3}\")\n", + "\n" ] }, { @@ -335,11 +514,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the elements are not in set1: {0, 65, 34, 72, 14, 51, 83, 55, 63, 62, 31}\n" + ] + } + ], "source": [ - "# Your code here" + "set4 = set2 - set1 \n", + "print(f\"the elements are not in set1: {set4}\")" ] }, { @@ -351,11 +539,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the elements common to set1 and set2: {2, 4, 6, 7, 8, 10, 15, 17, 20, 21, 22, 27, 28, 30, 33, 35, 37, 41, 43, 44, 46, 48, 49, 57, 64, 71, 73, 74, 75, 79, 82, 84, 85, 87, 88, 89, 92, 94, 95, 96, 97, 98}\n" + ] + } + ], "source": [ - "# Your code here" + "set5 = set1.intersection(set2)\n", + "print(f\"the elements common to set1 and set2: {set5}\")" ] }, { @@ -375,11 +572,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "len_set1: 80\n", + "len_set2: 53\n", + "len_set3: 38\n", + "len_set4: 11\n", + "len_set1 == len_set2 + len_set3 - len_set4: True\n" + ] + } + ], + "source": [ + "len_set1 = len(set1)\n", + "print(f\"len_set1: {len_set1}\")\n", + "len_set2 = len(set2) \n", + "print(f\"len_set2: {len_set2}\")\n", + "len_set3 = len(set3)\n", + "print(f\"len_set3: {len_set3}\")\n", + "len_set4 = len(set4)\n", + "print(f\"len_set4: {len_set4}\") \n", + "# formula : len_set1 = len_set2 + len_set3 - len_set4\n", + "print(f\"len_set1 == len_set2 + len_set3 - len_set4: {len_set1 == len_set2 + len_set3 - len_set4}\") " ] }, { @@ -391,11 +609,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set6 is empty: ()\n" + ] + } + ], "source": [ - "# Your code here" + "set6 = ()\n", + "print(f\"set6 is empty: {set6}\")" ] }, { @@ -407,11 +634,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set6 after union of set5 and set4: {0, 2, 4, 6, 7, 8, 10, 14, 15, 17, 20, 21, 22, 27, 28, 30, 31, 33, 34, 35, 37, 41, 43, 44, 46, 48, 49, 51, 55, 57, 62, 63, 64, 65, 71, 72, 73, 74, 75, 79, 82, 83, 84, 85, 87, 88, 89, 92, 94, 95, 96, 97, 98}\n" + ] + } + ], "source": [ - "# Your code here" + "set6 = set5 | set4\n", + "print(f\"set6 after union of set5 and set4: {set6}\")\n" ] }, { @@ -423,11 +659,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "checking if set6 is equal to set1: False\n" + ] + } + ], "source": [ - "# Your code here" + "print(f\"checking if set6 is equal to set1: {set6 == set1}\")" ] }, { @@ -439,11 +683,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set1 is not a subset of set2\n", + "set1 is not a subset of set3\n" + ] + } + ], + "source": [ + "if set1.issubset(set2):\n", + " print(f\"set1 is a subset of set2: {set1.issubset(set2)}\")\n", + "else:\n", + " print(f\"set1 is not a subset of set2\")\n", + "if set1.issubset(set3):\n", + " print(f\"set1 is a subset of set3: {set1.issubset(set3)}\")\n", + "else:\n", + " print(f\"set1 is not a subset of set3\") " ] }, { @@ -457,11 +717,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Set3_set4_set5: {0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 56, 57, 58, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99}\n", + "set1_set2: {0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 56, 57, 58, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99}\n", + "Set3_set4_set5 is equal to set1_set2: True\n" + ] + } + ], "source": [ - "# Your code here" + "Set3_set4_set5 = set3.union(set4).union(set5)\n", + "print(f\"Set3_set4_set5: {Set3_set4_set5}\")\n", + "set1_set2 = set1.union(set2)\n", + "print(f\"set1_set2: {set1_set2}\")\n", + "if Set3_set4_set5 == set1_set2:\n", + " print(f\"Set3_set4_set5 is equal to set1_set2: {Set3_set4_set5 == set1_set2}\")\n" ] }, { @@ -473,11 +748,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set1: {1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 32, 33, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 52, 53, 54, 56, 57, 58, 61, 64, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 81, 82, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99}\n", + "set1 removed the first element: {2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 32, 33, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 52, 53, 54, 56, 57, 58, 61, 64, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 81, 82, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99} \n" + ] + } + ], "source": [ - "# Your code here" + "print(f\"set1: {set1}\")\n", + "print(f\"set1 removed the first element: {set1 - {next(iter(set1))}} \")" ] }, { @@ -493,11 +778,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set1: {1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 32, 33, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 52, 53, 54, 56, 57, 58, 61, 64, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 81, 82, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99}\n", + "set1 after removing elements in list_to_remove: {2, 3, 4, 5, 6, 7, 8, 10, 13, 15, 16, 17, 18, 20, 22, 23, 24, 25, 26, 27, 28, 30, 32, 33, 35, 36, 37, 38, 42, 43, 44, 45, 46, 47, 48, 52, 53, 54, 56, 57, 58, 64, 66, 67, 68, 70, 73, 74, 75, 76, 77, 78, 82, 84, 85, 86, 87, 88, 90, 92, 93, 94, 95, 96, 97, 98}\n" + ] + } + ], "source": [ - "# Your code here" + "print(f\"set1: {set1}\")\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", + "set_after_removal = set1 - set(list_to_remove)\n", + "print(f\"set1 after removing elements in list_to_remove: {set_after_removal}\")" ] }, { @@ -515,7 +812,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -551,11 +848,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sorted word_freq2: OrderedDict({'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" + "from collections import OrderedDict\n", + "word_freq2 = OrderedDict(sorted(word_freq.items()))\n", + "print(f\"sorted word_freq2: {word_freq2}\")" ] }, { @@ -592,17 +899,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sorted_tuple: (('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", + "word_freq2: {'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", + "sorted_tuple = tuple(sorted(word_freq.items(), key=operator.itemgetter(1)))\n", + "print(f\"sorted_tuple: {sorted_tuple}\")\n", + "word_freq2 = {k: v for k, v in sorted_tuple}\n", + "print(f\"word_freq2: {word_freq2}\")\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -616,12 +936,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } + "version": "3.12.0" } }, "nbformat": 4,