diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 2e59d77..114f367 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "tup = (\"I\",)\n" ] }, { @@ -33,11 +35,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "type(tup)" ] }, { @@ -79,13 +93,66 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + }, + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here\n", + " #I used extend because of multiple elements\n", + "new_elements = (\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", "\n", - "# Your explanation here\n" + "tup_to_list= list(tup)\n", + "\n", + "\n", + "tup_to_list.extend(new_elements)\n", + "\n", + "back_to_tuple = tuple(tup_to_list)\n", + "\n", + "print(back_to_tuple)\n", + "type(back_to_tuple)\n", + "\n", + "# Tuples are unchangeable, or immutable as it also is called. But there is a workaround. It's possible to convert the tuple into a list, change the list, and convert the list back into a tuple.\n", + "# Or concatenate by adding new values\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], + "source": [ + "#Concatenation:\n", + "new_elements = (\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\",)\n", + "\n", + "conc_tup = tup + new_elements\n", + "\n", + "print(conc_tup)\n" ] }, { @@ -103,11 +170,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n')\n", + "('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "tup_1 = conc_tup[:4]\n", + "\n", + "print(tup_1)\n", + "\n", + "tup_2 = conc_tup[-4:]\n", + "\n", + "print(tup_2)\n" ] }, { @@ -121,11 +205,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup_3 = tup_1 + tup_2\n", + "\n", + "print(tup_3)" ] }, { @@ -137,11 +232,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup_1_count = len(tup_1)\n", + "tup_2_count = len(tup_2)\n", + "\n", + "tup_2_count+ tup_1_count == len(tup_3)\n" ] }, { @@ -153,11 +263,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "index = tup_3.index(\"h\")\n", + "\n", + "print(index)" ] }, { @@ -177,11 +298,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "False\n", + "False\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "for element in letters:\n", + " if element in tup_3:\n", + " print(True)\n", + " else:\n", + " print(False)" ] }, { @@ -195,12 +335,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 0, 1, 0, 0]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "count_letters = [tup_3.count(element) for element in letters]\n", + "print(count_letters)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -219,7 +376,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.11" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index cb3a3e0..5a8612f 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -13,11 +13,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ - "import random" + "import random\n" ] }, { @@ -38,11 +38,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[36, 3, 69, 68, 46, 93, 20, 37, 7, 4, 90, 67, 86, 41, 76, 92, 6, 59, 21, 82, 31, 29, 35, 11, 79, 0, 66, 30, 27, 34, 91, 94, 52, 71, 54, 99, 32, 13, 78, 50, 2, 74, 9, 10, 17, 23, 88, 42, 40, 15, 85, 51, 62, 57, 75, 45, 43, 16, 48, 77, 95, 44, 81, 8, 97, 22, 33, 24, 63, 26, 83, 98, 84, 58, 70, 73, 14, 28, 39, 80]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "sample_list_1 = random.sample(range(101),k=80)\n", + "\n", + "print(sample_list_1)" ] }, { @@ -54,11 +65,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set_1 = set(sample_list_1)\n", + "\n", + "print(len(list_to_set))" ] }, { @@ -77,11 +99,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[54, 26, 39, 91, 19, 12, 87, 29, 32, 45, 40, 61, 62, 59, 87, 77, 49, 66, 82, 41, 35, 38, 94, 77, 15, 67, 60, 74, 30, 87, 66, 86, 94, 5, 65, 95, 81, 58, 69, 29, 30, 84, 46, 82, 43, 66, 42, 19, 80, 16, 41, 72, 66, 52, 99, 7, 85, 82, 51, 0, 17, 73, 64, 22, 39, 42, 90, 19, 27, 45, 68, 32, 71, 3, 48, 38, 73, 61, 10, 88]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "list_2 = random.choices(range(101),k=80)\n", + "\n", + "print(sample_list_2)" ] }, { @@ -93,11 +126,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set_2 = set(list_2)\n", + "\n", + "print(len(set_2))" ] }, { @@ -109,11 +153,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 4, 6, 7, 8, 9, 11, 21, 22, 23, 26, 28, 32, 33, 39, 43, 44, 54, 57, 58, 63, 69, 75, 76, 78, 84, 93, 94, 95, 98, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set_3 = set_1 - set_2\n", + "\n", + "print(set_3)" ] }, { @@ -125,11 +180,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{64, 65, 18, 87, 60, 61}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set_4 = set_2 - set_1\n", + "\n", + "print(set_4)" ] }, { @@ -141,11 +207,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 10, 13, 14, 15, 16, 17, 20, 24, 27, 29, 30, 31, 34, 35, 36, 37, 40, 41, 42, 45, 46, 48, 50, 51, 52, 59, 62, 66, 67, 68, 70, 71, 73, 74, 77, 79, 80, 81, 82, 83, 85, 86, 88, 90, 91, 92, 97}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_5 = set.intersection(set_1,set_2)\n", + "print(set_5)" ] }, { @@ -157,11 +233,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_6 = set({})\n", + "print(set_6)" ] }, { @@ -173,11 +259,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39, 40, 41, 42, 43, 44, 45, 46, 48, 50, 51, 52, 54, 57, 58, 59, 62, 63, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 88, 90, 91, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set_6.update(set_3,set_5)\n", + "\n", + "print(set_6)" ] }, { @@ -189,11 +286,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_1 == set_6" ] }, { @@ -205,11 +314,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set_2.issubset(set_1)" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set_3.issubset(set_1)" ] }, { @@ -223,11 +364,52 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39, 40, 41, 42, 43, 44, 45, 46, 48, 50, 51, 52, 54, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "agg_set_1 = set_3.union(set_4,set_5)\n", + "\n", + "print(agg_set_1)" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39, 40, 41, 42, 43, 44, 45, 46, 48, 50, 51, 52, 54, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 97, 98, 99}\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "agg_set_2 = set_1.union(set_2)\n", + "print(agg_set_2)\n", + "\n", + "agg_set_1 == agg_set_2" ] }, { @@ -239,11 +421,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 90, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "first_element = set_1.pop()\n", + "print(first_element)" ] }, { @@ -259,11 +451,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 4, 6, 7, 8, 10, 13, 14, 15, 16, 17, 20, 22, 23, 24, 26, 27, 28, 30, 32, 33, 34, 35, 36, 37, 40, 42, 43, 44, 45, 46, 48, 50, 52, 54, 57, 58, 62, 63, 66, 67, 68, 70, 73, 74, 75, 76, 77, 78, 80, 82, 83, 84, 85, 86, 88, 90, 92, 93, 94, 95, 97, 98}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "\n", + "new_set = [set_1.remove(element) for element in list_to_remove if element in set_1]\n", + "\n", + "print(set_1)" ] }, { @@ -278,7 +483,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ @@ -290,9 +495,7 @@ "\n", "Carlos_list = ['tomatoes', 'water', 'onions', 'blueberries', 'garlic', 'flour', 'cherries', 'tomatoes', 'onions', 'water', 'tomatoes', 'toilet paper']\n", "\n", - "Mattia_list = []\n", - "\n", - "\n" + "Mattia_list = []\n" ] }, { @@ -304,11 +507,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 105, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "father_set = set(father_list)\n", + "jo_set = set(Jo_list)\n", + "carlos_set = set(Carlos_list)\n", + "matthia_set = set({})" ] }, { @@ -322,11 +529,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'flour', 'watermelon', 'gums', 'sugar', 'cherries', 'yogurt', 'water', 'juice', 'toilet paper', 'milk', 'cucumber'}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "matthia_set =carlos_set.symmetric_difference(jo_set)\n", + "print(matthia_set)" ] }, { @@ -338,9 +555,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['oranges', 'watermelon', 'water', 'onions', 'chocolate', 'mushrooms', 'onions', 'oranges', 'toilet paper', 'milk']\n" + ] + } + ], "source": [ "import random\n", "items = ['milk', 'water', 'chocolate', 'blueberries', 'shampoo', 'flour', 'bread', 'sugar', 'watermelon', 'vinegar', 'tomatoes', 'yogurt', 'juice', 'gums', 'onions', 'garlic', 'cucumber', 'mushrooms', 'toilet paper', 'oranges', 'deodorant', 'cherries']\n", @@ -352,11 +577,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 108, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'chocolate', 'watermelon', 'mushrooms', 'oranges', 'water', 'onions', 'milk', 'toilet paper'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "mother_set = set(mother_list)\n", + "\n", + "print(mother_set)" ] }, { @@ -373,11 +609,60 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 128, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "({'cucumber', 'flour', 'garlic', 'gums', 'onions', 'watermelon', 'yogurt'},\n", + " {'blueberries',\n", + " 'cucumber',\n", + " 'garlic',\n", + " 'gums',\n", + " 'juice',\n", + " 'milk',\n", + " 'onions',\n", + " 'sugar',\n", + " 'tomatoes',\n", + " 'watermelon',\n", + " 'yogurt'},\n", + " {'blueberries', 'cherries', 'flour', 'garlic', 'onions', 'tomatoes', 'water'},\n", + " {'chocolate',\n", + " 'milk',\n", + " 'mushrooms',\n", + " 'onions',\n", + " 'oranges',\n", + " 'water',\n", + " 'watermelon'},\n", + " {'cherries',\n", + " 'cucumber',\n", + " 'flour',\n", + " 'gums',\n", + " 'juice',\n", + " 'milk',\n", + " 'sugar',\n", + " 'water',\n", + " 'watermelon',\n", + " 'yogurt'})" + ] + }, + "execution_count": 128, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "all_sets = [father_set, jo_set, carlos_set, mother_set, matthia_set]\n", + "\n", + "for item in all_sets:\n", + " if 'toilet paper' in item:\n", + " item.discard('toilet paper')\n", + " else:\n", + " pass\n", + "\n", + "father_set, jo_set, carlos_set, mother_set, matthia_set" ] }, { @@ -389,11 +674,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 124, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'sugar', 'watermelon', 'gums', 'cherries', 'mushrooms', 'yogurt', 'milk', 'cucumber', 'flour', 'chocolate', 'blueberries', 'oranges', 'tomatoes', 'water', 'juice', 'onions', 'garlic'}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "all_set = father_set.union(jo_set,carlos_set,mother_set,matthia_set)\n", + "\n", + "print(all_set)\n" ] }, { @@ -407,11 +703,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 122, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "17" + ] + }, + "execution_count": 122, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "len(all_set)" + ] + }, + { + "cell_type": "code", + "execution_count": 135, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'chocolate', 'mushrooms', 'gums', 'juice', 'onions'}\n" + ] + } + ], + "source": [ + "import random\n", + "\n", + "all_set_list = list(all_set)\n", + "\n", + "while len(all_set_list)>5:\n", + " index_rand = random.randint(0, len(all_set_list)-1)\n", + " all_set_list.pop(index_rand)\n", + "\n", + "final_set = set(all_set_list)\n", + "\n", + "print(final_set)" ] } ], @@ -431,7 +766,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.10.11" } }, "nbformat": 4, diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index e041c1d..7711e64 100644 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -49,11 +49,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'about', 'all', 'although', 'and', 'are', 'at', 'baby', 'backseat', 'bag', 'bar', 'be', 'bedsheets', 'begin', 'best', 'body', 'boy', 'brand', 'can', 'chance', 'club', 'come', 'conversation', 'crazy', 'dance', 'date', 'day', 'discovering', 'do', 'doing', \"don't\", 'drinking', 'driver', 'eat', 'every', 'falling', 'family', 'fast', 'fill', 'find', 'first', 'follow', 'for', 'friends', 'get', 'girl', 'give', 'go', 'going', 'grab', 'hand', 'handmade', 'heart', 'hours', 'how', 'i', \"i'll\", \"i'm\", 'in', 'is', \"isn't\", 'it', 'jukebox', 'just', 'kiss', 'know', 'last', 'lead', 'leave', 'let', \"let's\", 'like', 'love', 'lover', 'magnet', 'make', 'man', 'may', 'me', 'mind', 'much', 'my', 'new', 'night', 'not', 'now', 'of', 'okay', 'on', 'one', 'our', 'out', 'over', 'place', 'plate', 'play', 'pull', 'push', 'put', 'radio', 'room', 'say', 'shape', 'shots', 'singing', 'slow', 'smell', 'so', 'somebody', 'something', 'sour', 'start', 'stop', 'story', 'sweet', 'table', 'take', 'talk', 'taxi', 'tell', 'that', 'the', 'then', 'thrifty', 'to', 'too', 'trust', 'up', 'van', 'waist', 'want', 'was', 'we', \"we're\", 'week', 'were', 'where', 'with', 'you', 'your']\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "keys = list(word_freq.keys())\n", + "keys.sort()\n", + "print(keys)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "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": [ + "word_freq2 = {}\n", + "\n", + "word_freq2 = {key: value for key, value in zip(keys, (word_freq[key] for key in keys))}\n", + "\n", + "print(word_freq2)" ] }, { @@ -90,11 +122,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "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", + "{'conversation': 1, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'going': 1, 'one': 1, 'backseat': 1, 'friends': 1, 'take': 1, 'play': 1, 'okay': 1, 'begin': 1, 'over': 1, 'just': 1, 'are': 1, 'tell': 1, 'drinking': 1, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, 'can': 1, 'club': 1, 'it': 1, 'out': 1, 'chance': 1, 'first': 1, 'table': 1, 'thrifty': 1, 'driver': 1, 'slow': 1, 'dance': 1, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'leave': 1, 'at': 1, 'hand': 1, 'how': 1, 'eat': 1, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'let': 1, 'van': 1, 'shots': 1, 'place': 1, 'find': 1, 'kiss': 1, 'stop': 1, 'bar': 1, \"don't\": 2, 'mind': 2, 'know': 2, 'so': 2, 'start': 2, 'boy': 2, 'girl': 2, 'singing': 2, 'doing': 2, 'somebody': 2, 'handmade': 2, 'may': 2, 'that': 2, 'much': 2, 'grab': 2, 'was': 2, 'say': 2, 'waist': 2, 'want': 2, \"let's\": 2, 'not': 2, 'crazy': 2, 'go': 2, 'to': 2, 'fill': 2, 'hours': 2, 'push': 3, 'then': 3, 'put': 3, 'room': 3, 'magnet': 3, 'up': 3, 'pull': 3, 'last': 3, 'do': 3, 'smell': 3, 'although': 3, 'falling': 3, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'bedsheets': 3, 'talk': 4, 'too': 5, 'is': 5, 'every': 6, 'new': 6, 'follow': 6, 'brand': 6, 'of': 6, 'i': 6, 'day': 6, 'lead': 6, 'shape': 6, 'discovering': 6, 'something': 6, 'we': 7, 'a': 8, 'like': 10, 'me': 10, 'now': 11, 'baby': 14, 'you': 16, 'be': 16, 'body': 17, 'the': 18, 'your': 21, 'with': 22, \"i'm\": 23, 'and': 23, 'love': 25, 'in': 27, 'my': 33, 'come': 37, 'on': 40}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "import operator\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "#print(sorted_tups)\n", + "\n", + "word_freq2 = {}\n", + "\n", + "word_freq2 = {key:value for key,value in sorted_tups}\n", + "\n", + "print(word_freq2)" ] }, { @@ -117,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -139,11 +190,83 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordfreq
0love25
1conversation1
2every6
3we're1
4plate1
\n", + "
" + ], + "text/plain": [ + " word freq\n", + "0 love 25\n", + "1 conversation 1\n", + "2 every 6\n", + "3 we're 1\n", + "4 plate 1" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df = pd.DataFrame(list(word_freq.items()), columns = [\"word\", \"freq\"])\n", + "\n", + "df.head()" ] }, { @@ -157,11 +280,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "120 a 8\n", + "109 about 1\n", + "43 all 1\n", + "96 although 3\n", + "72 and 23\n", + ".. ... ...\n", + "128 were 3\n", + "41 where 1\n", + "54 with 22\n", + "15 you 16\n", + "97 your 21\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df_1 = df.sort_values(by = ['word'])\n", + "print(df_1)" ] }, { @@ -173,12 +319,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " word freq\n", + "139 bar 1\n", + "114 let 1\n", + "116 van 1\n", + "60 out 1\n", + "57 it 1\n", + ".. ... ...\n", + "0 love 25\n", + "65 in 27\n", + "121 my 33\n", + "56 come 37\n", + "126 on 40\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df_2 = df.sort_values(by = ['freq'])\n", + "print(df_2)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -197,7 +373,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.8" + "version": "3.10.11" } }, "nbformat": 4,