diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 48f6b46..870b505 100755 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,12 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "tup=(\"I\",)" ] }, { @@ -33,11 +34,23 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "type(tup)" ] }, { @@ -55,13 +68,40 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'tuple' object has no attribute 'append'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Your code here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtup\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"r\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"o\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"h\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"a\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"c\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"k\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;31m#or, since append can only take one argument:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mnew_on_tuple\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"r\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"o\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"h\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"a\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"c\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"k\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mitem\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mnew_on_tuple\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" + ] + } + ], "source": [ "# Your code here\n", + "tup.append(\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "#or, since append can only take one argument:\n", + "new_on_tuple=[\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"]\n", + "for item in new_on_tuple:\n", + " tup.append(item)\n", + "tup\n", + "\n", + "# Your explanation here\n", "\n", - "# Your explanation here\n" + "#First of all, tuples are immutable, and no element can be added, changed or substracted.\n", + "#second, append is a list method\n", + "\n", + "list1=[\"I\"]\n", + "new_on_list=[\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"]\n", + "for item in new_on_list:\n", + " list1.append(item)\n", + "list1\n" ] }, { @@ -79,13 +119,31 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['I'] \n", + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k') \n" + ] + } + ], "source": [ "# Your code here\n", - "\n", - "# Your explanation here\n" + "tup=(\"I\",)\n", + "list2=list(tup)\n", + "print (list2, type(list2))\n", + "new_on_list=[\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"]\n", + "for item in new_on_list:\n", + " list2.append(item)\n", + "tup=tuple(list2)\n", + "print(tup, type(tup))\n", + "# Your explanation here\n", + "# I have created a list with the value of the tuple,\n", + "#added the new values, and turn that list into a tuple." ] }, { @@ -103,11 +161,26 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 45, "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", + "tup=(\"I\", \"r\", \"o\", \"n\",\"h\", \"a\", \"c\", \"k\")\n", + "tup1=tup[0:4]\n", + "\n", + "tup2=tup[-4:]\n", + "\n", + "print(tup1,\"\\n\", tup2)" ] }, { @@ -121,11 +194,24 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup3=tup1+tup2\n", + "tup3" ] }, { @@ -137,11 +223,27 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "l_tup1=len(tup1)\n", + "l_tup2=len(tup2)\n", + "\n", + "\n", + "l_tup1+l_tup2==len(tup3)" ] }, { @@ -153,11 +255,21 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "h_index=tup3.index(\"h\")\n", + "print(h_index)" ] }, { @@ -177,11 +289,25 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 59, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a True\n", + "c True\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "for item in letters:\n", + " if item in tup3:\n", + " print(item,True)\n", + " " ] }, { @@ -195,11 +321,25 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 63, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "counter=0\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "for item in letters:\n", + " if item in tup3:\n", + " counter+=1\n", + "print(counter)" ] }, { @@ -235,7 +375,49 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index ef7bde3..54d590f 100755 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -38,11 +38,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[52, 72, 63, 11, 81, 60, 88, 76, 41, 21, 30, 92, 90, 4, 45, 67, 51, 37, 53, 96, 94, 28, 82, 58, 6, 87, 74, 8, 22, 65, 83, 5, 15, 68, 62, 26, 91, 54, 38, 79, 77, 59, 34, 24, 29, 47, 42, 98, 48, 19, 25, 17, 18, 20, 1, 86, 40, 3, 84, 57, 95, 33, 97, 55, 71, 99, 75, 85, 23, 93, 89, 31, 73, 43, 69, 50, 66, 2, 32, 10]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "sample_list_1=[]\n", + "population=range(0,100)\n", + "k=80\n", + "sample_list_1= random.sample(population, k)\n", + "print(sample_list_1)\n", + "\n" ] }, { @@ -54,11 +68,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "{1, 2, 3, 4, 5, 6, 8, 10, 11, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 34, 37, 38, 40, 41, 42, 43, 45, 47, 48, 50, 51, 52, 53, 54, 55, 57, 58, 59, 60, 62, 63, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set1=(set(sample_list_1))\n", + "print(len(set1))\n", + "print(set1)" ] }, { @@ -77,11 +103,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[7, 16, 38, 24, 75, 33, 90, 64, 52, 50, 85, 68, 18, 33, 17, 20, 20, 28, 33, 30, 99, 74, 46, 48, 53, 59, 48, 60, 87, 55, 31, 95, 32, 85, 71, 7, 62, 95, 89, 5, 63, 11, 68, 22, 96, 79, 20, 56, 78, 55, 62, 89, 52, 88, 28, 36, 83, 63, 80, 35, 28, 33, 7, 0, 45, 64, 91, 62, 75, 0, 15, 21, 69, 59, 69, 5, 58, 37, 23, 3]\n" + ] + }, + { + "data": { + "text/plain": [ + "80" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "sample_list_2=[]\n", + "for i in range(0,80):\n", + " sample_list_2.append(random.randrange(100))\n", + "print(sample_list_2)\n", + "len(sample_list_2)" ] }, { @@ -93,11 +142,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set2=(set(sample_list_2))\n", + "print(len(set2))" ] }, { @@ -109,11 +168,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 4, 6, 8, 10, 19, 25, 26, 29, 34, 40, 41, 42, 43, 47, 51, 54, 57, 65, 66, 67, 72, 73, 76, 77, 81, 82, 84, 86, 92, 93, 94, 97, 98}\n", + "35\n" + ] + } + ], + "source": [ + "# Your code here\n", + "list3=[]\n", + "for item in set1:\n", + " if item not in set2:\n", + " list3.append(item)\n", + "set3=set(list3)\n", + "print(set3)\n", + "print(len(set3))" ] }, { @@ -125,11 +200,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 7, 16, 35, 36, 46, 56, 64, 78, 80]\n", + "10\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set4=[]\n", + "for item in set2:\n", + " if item not in set1:\n", + " set4.append(item)\n", + "print(set4)\n", + "print(len(set4))" ] }, { @@ -141,11 +231,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "#starting point:\n", + "#print(\"set1:\\t\",set1)\n", + "#print(\"set2:\\t\",set2)\n", + "list5=[]\n", + "\n", + "for item in set1:\n", + " if item in set2:\n", + " list5.append(item)\n", + "\n", + "#checks:\n", + "#print(\"set1:\\t\",set1,\"\\n\",len(set1))\n", + "#print(\"set2:\\t\",set2,\"\\n\",len(set2)) \n", + "#print(\"list5:\\t\",list5,\"\\n\",len(list5))\n", + "set5=set(list5) \n", + "#print(\"set5:\\t\",set5,\"\\n\",len(set5))\n", + "\n" ] }, { @@ -165,11 +272,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "55\n", + "35\n", + "10\n", + "45\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "#set1: 80 unique random numbers from 0 to 100\n", + "print(len(set1))\n", + "#set2: 80 random numbers from 0 to 100, minus repeats, 57 numbers in my case\n", + "print(len(set2))\n", + "#set3: elements in set1 but not in set2\n", + "print(len(set3))\n", + "#set4:elements in set2 but not in set1\n", + "print(len(set4))\n", + "#set5:intersection between set 1 and 2\n", + "print(len(set5))\n", + "\n", + "\n" ] }, { @@ -181,11 +313,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "set6=set()" ] }, { @@ -197,11 +330,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "80\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set6=set()\n", + "print(len(set6))\n", + "set6.update(set5,set3)\n", + "print(len(set6))\n" ] }, { @@ -213,11 +359,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set1==set6" ] }, { @@ -229,11 +387,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set1.issubset(set2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set2.issubset(set1)" ] }, { @@ -247,11 +437,40 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "lenths of individual sets 3, 4 and 5: 90\n", + "lenths of len7 (agregate): 90\n", + "55\n", + "80\n", + "lenths of individual sets 1 and 2: 135\n", + "lenths of len8 (agregate): 90\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set7=set()\n", + "\n", + "#print(\"len set5\",len(set5), \"len set4\",len(set4),\"len set3\", len(set3))\n", + "print(\"lenths of individual sets 3, 4 and 5: \",len(set3)+len(set4)+len(set5))\n", + "set7 = set3.union(set4,set5)\n", + "print(\"lenths of len7 (agregate): \",len(set7))\n", + "\n", + "\n", + "##\n", + "\n", + "\n", + "print(len(set2))\n", + "print(len(set1))\n", + "print(\"lenths of individual sets 1 and 2: \",len(set1)+len(set2))\n", + "set8 = set1.union(set2)\n", + "print(\"lenths of len8 (agregate): \",len(set8))" ] }, { @@ -263,11 +482,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "79\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(len(set1))\n", + "list_25=list(set1)\n", + "\n", + "list_25.pop(-1)\n", + "set1=set(list_25)\n", + "\n", + "print(len(set1))\n", + "\n" ] }, { @@ -281,14 +517,45 @@ "```" ] }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "{1, 2, 3, 4, 5, 6, 8, 10, 11, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 34, 37, 38, 40, 41, 42, 43, 45, 47, 48, 50, 51, 52, 53, 54, 55, 57, 58, 59, 60, 62, 63, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99}\n", + "20\n", + "64\n", + "20\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(len(set1))\n", + "print(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", + "#There is no overlap to beguin with.\n", + "\n", + "print(len(list_to_remove))\n", + "set_to_remove=set(list_to_remove)\n", + "set1.difference_update(set_to_remove)\n", + "\n", + "print(len(set1))\n", + "\n", + "print(len(set_to_remove))" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here\n" - ] + "source": [] } ], "metadata": { @@ -307,7 +574,49 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false } }, "nbformat": 4, diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 74d65e0..db44cd3 100755 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -21,11 +21,24 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "dict1 = {'Penelope':178, 'Arnau':172, 'Marta':175}\n", + "type(dict1)" ] }, { @@ -37,11 +50,61 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['Penelope', 'Arnau', 'Marta'])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "dict1.keys()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_values([178, 172, 175])\n" + ] + } + ], + "source": [ + "# Your code here\n", + "dict1.values()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('Penelope', 178), ('Arnau', 172), ('Marta', 175)])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict1.items()" ] }, { @@ -53,11 +116,21 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_items([('Penelope', 178), ('Arnau', 172), ('Marta', 175), ('John', 180)])\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "dict1[\"John\"]=180\n", + "print(dict1.items())" ] }, { @@ -69,11 +142,21 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_items([('Penelope', 178), ('Arnau', 172), ('Marta', 175)])\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "del dict1[\"John\"]\n", + "print(dict1.items())" ] }, { @@ -87,7 +170,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 42, "metadata": {}, "outputs": [], "source": [ @@ -121,11 +204,37 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 50, "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\n" + "# Your code here\n", + "\n", + "keys=list(word_freq.keys())\n", + "#keys=list(keys)\n", + "keys.sort()\n", + "#print(keys)\n", + "word_freq2=dict()\n", + "#Use a FOR loop to iterate each value in keys.\n", + "#For each key iterated, find the corresponding value in word_freq\n", + "#and insert the key-value pair to word_freq2.\n", + "\n", + "#update is similiar to append but for lists.\n", + "#get is how you retrieve a value for a key\n", + "#https://www.w3schools.com/python/ref_dictionary_get.asp\n", + "\n", + "for word in keys:\n", + " word_freq2.update({word:word_freq.get(word)})\n", + "\n", + "print(word_freq2)" ] }, { @@ -162,12 +271,46 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], + "execution_count": 54, + "metadata": { + "scrolled": false + }, + "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\n" + "# Your code here\n", + "import operator\n", + "\n", + "\n", + "\n", + "#Therefore, the steps to sort word_freq by value are:\n", + "#Using sorted and operator.itemgetter,\n", + "#obtain a list of tuples of the dict key-value pairs which is sorted on the value.\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "#print(sorted_tups)\n", + "#Create an empty dictionary named word_freq2.\n", + "word_freq2=dict()\n", + "#Iterate the list of tuples.\n", + "\n", + "#Insert each key-value pair into word_freq2 as an object.\n", + "for tup in sorted_tups:\n", + " word_freq2.update({tup[0]:(tup[1])})\n", + "print(word_freq2)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -186,7 +329,49 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false } }, "nbformat": 4,