diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5aed049 Binary files /dev/null and b/.DS_Store differ diff --git a/your-code/challenges.ipynb b/your-code/challenges.ipynb index ba91b3f..de4015e 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\",)" ] }, { @@ -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))" ] }, { @@ -53,16 +61,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "scrolled": true }, - "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", - "# You can :) " + "tup = tup + (\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "print(tup)" ] }, { @@ -80,11 +94,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")" ] }, { @@ -102,11 +116,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tup1: ('I', 'r', 'o', 'n')\n", + "tup2: ('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here" + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "\n", + "# Split using slicing\n", + "tup1 = tup[:4] # positive indices (start to index 3)\n", + "tup2 = tup[-4:] # negative indices (last 4 elements)\n", + "\n", + "# Print the results\n", + "print(\"tup1:\", tup1)\n", + "print(\"tup2:\", tup2)\n" ] }, { @@ -120,11 +151,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tup3: ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n", + "tup3 equals tup: True\n", + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here" + "# Combine tup1 and tup2\n", + "tup3 = tup1 + tup2\n", + "\n", + "# Print the result\n", + "print(\"tup3:\", tup3)\n", + "\n", + "# Check if tup3 equals tup\n", + "print(\"tup3 equals tup:\", tup3 == tup)\n", + "print(tup)\n" ] }, { @@ -136,11 +185,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Count of tup1: 4\n", + "Count of tup2: 4\n", + "Sum of counts: 8\n", + "Count of tup3: 8\n", + "Is sum equal to count of tup3? True\n" + ] + } + ], "source": [ - "# Your code here" + "# Count elements in each tuple\n", + "count_tup1 = len(tup1)\n", + "count_tup2 = len(tup2)\n", + "count_tup3 = len(tup3)\n", + "\n", + "# Add the counts of tup1 and tup2\n", + "sum_counts = count_tup1 + count_tup2\n", + "\n", + "# Print results\n", + "print(\"Count of tup1:\", count_tup1)\n", + "print(\"Count of tup2:\", count_tup2)\n", + "print(\"Sum of counts:\", sum_counts)\n", + "print(\"Count of tup3:\", count_tup3)\n", + "print(\"Is sum equal to count of tup3?\", sum_counts == count_tup3)\n" ] }, { @@ -152,11 +226,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Index of 'h' in tup3: 4\n", + "\n" + ] + } + ], "source": [ - "# Your code here" + "index_h = tup3.index(\"h\")\n", + "print(\"Index of 'h' in tup3:\", index_h)\n", + "print(type(tup))" ] }, { @@ -176,20 +261,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a True\n", + "b False\n", + "c True\n", + "d False\n", + "e False\n" + ] + } + ], "source": [ - "# Your code here" + "for letter in letters:\n", + " print(letter, letter in tup3)" ] }, { @@ -203,11 +303,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "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" + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "for letter in letters:\n", + " print(f\"{letter}: {tup3.count(letter)}\")\n" ] }, { @@ -223,7 +338,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -248,11 +363,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6, 17, 74, 65, 27, 93, 76, 4, 89, 13, 95, 87, 44, 75, 38, 19, 86, 34, 78, 51, 58, 2, 70, 57, 49, 42, 50, 35, 77, 61, 80, 82, 52, 18, 23, 10, 88, 96, 41, 14, 98, 83, 90, 92, 24, 84, 53, 39, 54, 60, 36, 0, 32, 94, 28, 64, 81, 48, 7, 16, 9, 12, 3, 25, 56, 1, 33, 69, 63, 45, 72, 8, 85, 20, 43, 30, 73, 29, 26, 79]\n" + ] + } + ], "source": [ - "# Your code here" + "import random\n", + "\n", + "# Create a list with 80 unique random integers between 0 and 100\n", + "sample_list_1 = random.sample(range(0, 101), 80)\n", + "\n", + "# Print the list\n", + "print(sample_list_1)" ] }, { @@ -264,11 +393,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n" + ] + } + ], "source": [ - "# your code here" + "# Convert sample_list_1 to a set\n", + "set1 = set(sample_list_1)\n", + "\n", + "# Print the length of the set\n", + "print(len(set1))" ] }, { @@ -287,11 +428,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[34, 83, 95, 14, 99, 43, 73, 98, 10, 85, 98, 69, 57, 37, 93, 28, 21, 18, 96, 69, 22, 9, 46, 75, 53, 67, 5, 18, 86, 80, 26, 23, 76, 52, 11, 62, 10, 9, 18, 67, 59, 7, 86, 50, 59, 89, 54, 53, 92, 41, 53, 49, 87, 91, 58, 8, 12, 38, 18, 43, 8, 0, 30, 59, 27, 79, 54, 84, 24, 76, 92, 67, 52, 26, 61, 67, 90, 70, 14, 83]\n" + ] + } + ], "source": [ - "# your code here" + "# Create an empty list\n", + "sample_list_2 = []\n", + "\n", + "# Use a FOR loop to add 80 random values\n", + "for i in range(80):\n", + " sample_list_2.append(random.randint(0, 100))\n", + "\n", + "# Print the list\n", + "print(sample_list_2)" ] }, { @@ -303,11 +460,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "56\n" + ] + } + ], "source": [ - "# Your code here" + "# Convert sample_list_2 to a set\n", + "set2 = set(sample_list_2)\n", + "\n", + "# Print the length of the set\n", + "print(len(set2))\n" ] }, { @@ -319,11 +488,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 3, 4, 6, 13, 16, 17, 19, 20, 25, 29, 32, 33, 35, 36, 39, 42, 44, 45, 48, 51, 56, 60, 63, 64, 65, 72, 74, 77, 78, 81, 82, 88, 94}\n", + "Length of set3: 35\n" + ] + } + ], "source": [ - "# Your code here" + "# Find elements in set1 but not in set2\n", + "set3 = set1 - set2\n", + "\n", + "# Print the result\n", + "print(set3)\n", + "print(f\"Length of set3: {len(set3)}\")" ] }, { @@ -335,11 +518,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{67, 99, 37, 5, 91, 11, 46, 21, 22, 59, 62}\n", + "Length of set4: 11\n" + ] + } + ], "source": [ - "# Your code here" + "# Find elements in set2 but not in set1\n", + "set4 = set2 - set1\n", + "\n", + "# Print the result\n", + "print(set4)\n", + "print(f\"Length of set4: {len(set4)}\")" ] }, { @@ -351,11 +548,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 7, 8, 9, 10, 12, 14, 18, 23, 24, 26, 27, 28, 30, 34, 38, 41, 43, 49, 50, 52, 53, 54, 57, 58, 61, 69, 70, 73, 75, 76, 79, 80, 83, 84, 85, 86, 87, 89, 90, 92, 93, 95, 96, 98}\n", + "Length of set5: 45\n" + ] + } + ], "source": [ - "# Your code here" + "# Find elements shared between set1 and set2\n", + "set5 = set1 & set2\n", + "\n", + "# Print the result\n", + "print(set5)\n", + "print(f\"Length of set5: {len(set5)}\")" ] }, { @@ -375,11 +586,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Formula 1: len(set1) = len(set3) + len(set5)\n", + "len(set1) = 80\n", + "len(set3) + len(set5) = 80\n", + "Equal? True\n", + "\n", + "Formula 2: len(set2) = len(set4) + len(set5)\n", + "len(set2) = 56\n", + "len(set4) + len(set5) = 56\n", + "Equal? True\n", + "\n", + "Formula 3: len(set1 | set2) = len(set3) + len(set4) + len(set5)\n", + "len(set1 | set2) = 91\n", + "len(set3) + len(set4) + len(set5) = 91\n", + "Equal? True\n", + "\n", + "Formula 4: len(set1) + len(set2) = len(set3) + len(set4) + 2*len(set5)\n", + "len(set1) + len(set2) = 136\n", + "len(set3) + len(set4) + 2*len(set5) = 136\n", + "Equal? True\n" + ] + } + ], "source": [ - "# Your code here" + "# Test Formula 1: len(set1) = len(set3) + len(set5)\n", + "print(\"Formula 1: len(set1) = len(set3) + len(set5)\")\n", + "print(f\"len(set1) = {len(set1)}\")\n", + "print(f\"len(set3) + len(set5) = {len(set3) + len(set5)}\")\n", + "print(f\"Equal? {len(set1) == len(set3) + len(set5)}\")\n", + "print()\n", + "\n", + "# Test Formula 2: len(set2) = len(set4) + len(set5)\n", + "print(\"Formula 2: len(set2) = len(set4) + len(set5)\")\n", + "print(f\"len(set2) = {len(set2)}\")\n", + "print(f\"len(set4) + len(set5) = {len(set4) + len(set5)}\")\n", + "print(f\"Equal? {len(set2) == len(set4) + len(set5)}\")\n", + "print()\n", + "\n", + "# Test Formula 3: len(set1 | set2) = len(set3) + len(set4) + len(set5)\n", + "print(\"Formula 3: len(set1 | set2) = len(set3) + len(set4) + len(set5)\")\n", + "print(f\"len(set1 | set2) = {len(set1 | set2)}\")\n", + "print(f\"len(set3) + len(set4) + len(set5) = {len(set3) + len(set4) + len(set5)}\")\n", + "print(f\"Equal? {len(set1 | set2) == len(set3) + len(set4) + len(set5)}\")\n", + "print()\n", + "\n", + "# Test Formula 4: len(set1) + len(set2) = len(set3) + len(set4) + 2*len(set5)\n", + "print(\"Formula 4: len(set1) + len(set2) = len(set3) + len(set4) + 2*len(set5)\")\n", + "print(f\"len(set1) + len(set2) = {len(set1) + len(set2)}\")\n", + "print(f\"len(set3) + len(set4) + 2*len(set5) = {len(set3) + len(set4) + 2*len(set5)}\")\n", + "print(f\"Equal? {len(set1) + len(set2) == len(set3) + len(set4) + 2*len(set5)}\")" ] }, { @@ -391,11 +653,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n", + "Type: \n", + "Length: 0\n" + ] + } + ], "source": [ - "# Your code here" + "# Create an empty set\n", + "set6 = set()\n", + "\n", + "# Print to verify\n", + "print(set6)\n", + "print(f\"Type: {type(set6)}\")\n", + "print(f\"Length: {len(set6)}\")" ] }, { @@ -407,11 +685,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18, 19, 20, 23, 24, 25, 26, 27, 28, 29, 30, 32, 33, 34, 35, 36, 38, 39, 41, 42, 43, 44, 45, 48, 49, 50, 51, 52, 53, 54, 56, 57, 58, 60, 61, 63, 64, 65, 69, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 96, 98}\n", + "Length of set6: 80\n" + ] + } + ], "source": [ - "# Your code here" + "# Add set3 and set5 to set6 using update method\n", + "set6.update(set3, set5)\n", + "\n", + "# Print to verify\n", + "print(set6)\n", + "print(f\"Length of set6: {len(set6)}\")" ] }, { @@ -423,11 +715,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ - "# Your code here" + "# Check if set1 and set6 are equal\n", + "print(set1 == set6)" ] }, { @@ -439,11 +740,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Is set2 a subset of set1? False\n", + "Is set3 a subset of set1? True\n" + ] + } + ], "source": [ - "# Your code here" + "# Check if set1 contains set2 (i.e., is set2 a subset of set1?)\n", + "print(f\"Is set2 a subset of set1? {set2.issubset(set1)}\")\n", + "\n", + "# Check if set1 contains set3 (i.e., is set3 a subset of set1?)\n", + "print(f\"Is set3 a subset of set1? {set3.issubset(set1)}\")" ] }, { @@ -457,11 +771,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Are the aggregated sets equal? True\n", + "\n", + "Length of set3 ∪ set4 ∪ set5: 91\n", + "Length of set1 ∪ set2: 91\n" + ] + } + ], "source": [ - "# Your code here" + "# Aggregate set3, set4, and set5 using union method\n", + "aggregated_set_345 = set3.union(set4, set5)\n", + "\n", + "# Aggregate set1 and set2 using union method\n", + "aggregated_set_12 = set1.union(set2)\n", + "\n", + "# Check if the aggregated values are equal\n", + "print(f\"Are the aggregated sets equal? {aggregated_set_345 == aggregated_set_12}\")\n", + "\n", + "# Print additional information\n", + "print(f\"\\nLength of set3 ∪ set4 ∪ set5: {len(aggregated_set_345)}\")\n", + "print(f\"Length of set1 ∪ set2: {len(aggregated_set_12)}\")" ] }, { @@ -473,11 +809,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Removed element: 0\n", + "Length of set1 after pop: 79\n" + ] + } + ], "source": [ - "# Your code here" + "# Remove an element from set1 using pop method\n", + "removed_element = set1.pop()\n", + "\n", + "# Print the removed element and the updated set\n", + "print(f\"Removed element: {removed_element}\")\n", + "print(f\"Length of set1 after pop: {len(set1)}\")" ] }, { @@ -493,11 +843,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 4, 6, 7, 8, 10, 12, 13, 14, 16, 17, 18, 20, 23, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 42, 43, 44, 45, 48, 50, 52, 53, 54, 56, 57, 58, 60, 63, 64, 65, 70, 72, 73, 74, 75, 76, 77, 78, 80, 82, 83, 84, 85, 86, 87, 88, 90, 92, 93, 94, 95, 96, 98}\n", + "Length of set1 after removal: 66\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", + "# Remove elements from set1 if they are present\n", + "set1.difference_update(list_to_remove)\n", + "\n", + "# Print the remaining elements\n", + "print(set1)\n", + "print(f\"Length of set1 after removal: {len(set1)}\")" ] }, { @@ -515,7 +881,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ @@ -551,11 +917,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "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" + "# Original dictionary\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: Extract the 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: Use a FOR loop to populate word_freq2 with sorted keys\n", + "for key in keys:\n", + " word_freq2[key] = word_freq[key]\n", + "\n", + "# Print the result\n", + "print(word_freq2)" ] }, { @@ -592,17 +983,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "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" + "import operator\n", + "\n", + "# Original dictionary\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 the dictionary items by value using sorted and operator.itemgetter\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "\n", + "# Step 2: Create an empty dictionary\n", + "word_freq2 = {}\n", + "\n", + "# Step 3: Iterate through the sorted tuples and populate word_freq2\n", + "for key, value in sorted_tups:\n", + " word_freq2[key] = value\n", + "\n", + "# Print the result\n", + "print(word_freq2)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -616,12 +1031,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } + "version": "3.13.7" } }, "nbformat": 4,