From af3a89936433001b42117496fb46b3ce70cf7c35 Mon Sep 17 00:00:00 2001 From: Jyu-as Date: Tue, 20 Oct 2020 19:46:12 +0200 Subject: [PATCH 1/2] Lab updated but not finished --- your-code/challenge-1.ipynb | 233 ++++++++++++++++++--- your-code/challenge-2.ipynb | 401 +++++++++++++++++++++++++++++++----- your-code/challenge-3.ipynb | 126 +++++++++-- 3 files changed, 666 insertions(+), 94 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 48f6b46..08a49ce 100755 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,11 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "tup = ('I',)" ] }, { @@ -33,11 +33,22 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "type(tup)" ] }, { @@ -55,13 +66,32 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'tuple' object has no attribute 'append'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtup\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"o\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"n\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"h\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"a\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"c\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"k\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" + ] + } + ], + "source": [ + "tup.append(\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")" + ] + }, + { + "cell_type": "code", + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n", - "\n", - "# Your explanation here\n" + "#Append method does not work with tuples becasue they are inmutable." ] }, { @@ -79,13 +109,12 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n", - "\n", - "# Your explanation here\n" + "tup = ('I','r','o','n','h','a','c','k')\n", + "# For re-assigning we create a new taple because tuples are inmutables and they do not work like lists." ] }, { @@ -103,11 +132,24 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 57, "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", + "tup1 = tup[0:4]\n", + "print(tup1)\n", + "tup2 = tup[-4:]\n", + "print(tup2)" ] }, { @@ -121,11 +163,20 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 59, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup3 = tup1 + tup2\n", + "print(tup3)" ] }, { @@ -137,11 +188,33 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 79, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "t1 = len(tup1)\n", + "t2 = len(tup2)\n", + "t3 = len(tup3)" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t1 + t2 == t3" ] }, { @@ -153,11 +226,22 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 81, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "tup3.index('h')" ] }, { @@ -177,11 +261,31 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a is in tup3\n", + "c is in tup3\n" + ] + } + ], + "source": [ + "for i in letters:\n", + " if i in tup3:\n", + " print(i ,'is in tup3')" ] }, { @@ -195,11 +299,31 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 100, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a appears:\n", + "1\n", + "b appears:\n", + "0\n", + "c appears:\n", + "1\n", + "d appears:\n", + "0\n", + "e appears:\n", + "0\n" + ] + } + ], "source": [ - "# Your code here\n" + "for i in letters:\n", + " print(i,'appears:')\n", + " print(tup3.count(i))\n", + " " ] }, { @@ -211,12 +335,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 103, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "11\n", + "12\n", + "13\n", + "14\n", + "15\n", + "16\n", + "17\n", + "18\n", + "19\n" + ] + } + ], "source": [ - "# Your code here\n" + "a, b, c, d, e, f, g, h, i, j = range(10,20)\n", + "\n", + "print(a)\n", + "print(b)\n", + "print(c)\n", + "print(d)\n", + "print(e)\n", + "print(f)\n", + "print(g)\n", + "print(h)\n", + "print(i)\n", + "print(j)\n", + "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -235,7 +402,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.7.6" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index ef7bde3..688c0dd 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": 17, "metadata": {}, "outputs": [], "source": [ @@ -38,11 +38,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[8, 65, 39, 86, 60, 48, 41, 45, 51, 94, 75, 89, 88, 42, 87, 1, 70, 93, 28, 98, 83, 17, 9, 34, 19, 64, 35, 50, 26, 53, 2, 20, 54, 16, 40, 96, 92, 95, 61, 52, 47, 99, 43, 82, 18, 36, 44, 32, 69, 62, 22, 30, 23, 38, 46, 4, 67, 85, 59, 14, 73, 68, 6, 55, 79, 78, 84, 15, 66, 58, 56, 81, 90, 27, 13, 74, 91, 24, 10, 25]\n" + ] + } + ], "source": [ - "# Your code here\n" + "sample_list_1 = random.sample(range(100),80)\n", + "print(sample_list_1)" ] }, { @@ -54,11 +63,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n" + ] + } + ], "source": [ - "# Your code here\n" + "set1 = set(sample_list_1)\n", + "print(len(set1))" ] }, { @@ -77,11 +95,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.randrange(1,10)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[66, 39, 73, 10, 2, 0, 48, 89, 88, 21, 94, 31, 43, 39, 62, 21, 47, 55, 28, 30, 66, 79, 62, 60, 57, 61, 77, 38, 49, 65, 61, 69, 90, 74, 15, 32, 6, 89, 52, 10, 63, 37, 81, 42, 14, 93, 86, 59, 3, 13, 62, 59, 1, 78, 94, 67, 14, 11, 9, 40, 53, 9, 24, 73, 39, 95, 63, 92, 25, 47, 78, 44, 77, 86, 21, 81, 51, 35, 6, 27]\n", + "80\n" + ] + } + ], "source": [ - "# Your code here\n" + "sample_list_2 = []\n", + "for i in range(80):\n", + " z = random.randrange(0,100)\n", + " sample_list_2.append(z)\n", + "print(sample_list_2)\n", + "print(len(sample_list_2))\n" ] }, { @@ -93,11 +145,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "58\n" + ] + } + ], "source": [ - "# Your code here\n" + "set2 = set(sample_list_2)\n", + "print(len(set2))" ] }, { @@ -109,11 +170,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 4, 6, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 30, 32, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 70, 73, 74, 75, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99}\n", + "{0, 1, 2, 3, 6, 9, 10, 11, 13, 14, 15, 21, 24, 25, 27, 28, 30, 31, 32, 35, 37, 38, 39, 40, 42, 43, 44, 47, 48, 49, 51, 52, 53, 55, 57, 59, 60, 61, 62, 63, 65, 66, 67, 69, 73, 74, 77, 78, 79, 81, 86, 88, 89, 90, 92, 93, 94, 95}\n" + ] + } + ], + "source": [ + "print(set1)\n", + "print(set2)" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{4, 8, 16, 17, 18, 19, 20, 22, 23, 26, 34, 36, 41, 45, 46, 50, 54, 56, 58, 64, 68, 70, 75, 82, 83, 84, 85, 87, 91, 96, 98, 99}\n" + ] + } + ], + "source": [ + "list3 = []\n", + "for s in set1:\n", + " if s not in set2:\n", + " list3.append(s)\n", + " set3 = set(list3)\n", + " \n", + "print(set3)\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{4, 8, 16, 17, 18, 19, 20, 22, 23, 26, 34, 36, 41, 45, 46, 50, 54, 56, 58, 64, 68, 70, 75, 82, 83, 84, 85, 87, 91, 96, 98, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set3= set1 - set2\n", + "print(set3)" ] }, { @@ -125,11 +239,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 3, 37, 11, 77, 49, 21, 57, 63, 31}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set4 = set2 - set1\n", + "print(set4)" ] }, { @@ -141,11 +264,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 6, 9, 10, 13, 14, 15, 24, 25, 27, 28, 30, 32, 35, 38, 39, 40, 42, 43, 44, 47, 48, 51, 52, 53, 55, 59, 60, 61, 62, 65, 66, 67, 69, 73, 74, 78, 79, 81, 86, 88, 89, 90, 92, 93, 94, 95}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set5 = set1.intersection(set2)\n", + "print(set5)" ] }, { @@ -165,11 +297,52 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 98, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 4, 6, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 30, 32, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 70, 73, 74, 75, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99}\n", + "{0, 1, 2, 3, 6, 9, 10, 11, 13, 14, 15, 21, 24, 25, 27, 28, 30, 31, 32, 35, 37, 38, 39, 40, 42, 43, 44, 47, 48, 49, 51, 52, 53, 55, 57, 59, 60, 61, 62, 63, 65, 66, 67, 69, 73, 74, 77, 78, 79, 81, 86, 88, 89, 90, 92, 93, 94, 95}\n", + "{4, 8, 16, 17, 18, 19, 20, 22, 23, 26, 34, 36, 41, 45, 46, 50, 54, 56, 58, 64, 68, 70, 75, 82, 83, 84, 85, 87, 91, 96, 98, 99}\n", + "{0, 3, 37, 11, 77, 49, 21, 57, 63, 31}\n", + "{1, 2, 6, 9, 10, 13, 14, 15, 24, 25, 27, 28, 30, 32, 35, 38, 39, 40, 42, 43, 44, 47, 48, 51, 52, 53, 55, 59, 60, 61, 62, 65, 66, 67, 69, 73, 74, 78, 79, 81, 86, 88, 89, 90, 92, 93, 94, 95}\n" + ] + } + ], + "source": [ + "print(set1)\n", + "print(set2)\n", + "print(set3)\n", + "print(set4)\n", + "print(set5)" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "58\n", + "32\n", + "10\n", + "48\n" + ] + } + ], + "source": [ + "print(len(set1))\n", + "print(len(set2))\n", + "print(len(set3))\n", + "print(len(set4))\n", + "print(len(set5))" ] }, { @@ -181,11 +354,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "set6 = set()" ] }, { @@ -197,11 +370,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 108, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 4, 6, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 30, 32, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 70, 73, 74, 75, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set6.update(set3,set5)\n", + "print(set6)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 4, 6, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 30, 32, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 70, 73, 74, 75, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99}\n" + ] + } + ], + "source": [ + "set7 = set3.union(set5)\n", + "print(set7)" ] }, { @@ -213,11 +413,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 114, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 114, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "set1 == set6\n" ] }, { @@ -229,11 +440,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 117, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "False\n" + ] + } + ], "source": [ - "# Your code here\n" + "print(set1.issubset(set2))\n", + "print(set1.issubset(set3))" ] }, { @@ -247,11 +468,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 121, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 2, 3, 4, 6, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 73, 74, 75, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99}\n", + "{0, 1, 2, 3, 4, 6, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 73, 74, 75, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99}\n" + ] + } + ], + "source": [ + "agreggate1 = set3.union(set4,set5)\n", + "print(agreggate1)\n", + "\n", + "agreggate2 = set1.union(set2)\n", + "print(agreggate2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 122, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 122, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "agreggate1 == agreggate2" ] }, { @@ -263,11 +517,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 126, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ - "# Your code here\n" + "drop_val = set1.pop()\n", + "print(drop_val)" ] }, { @@ -283,11 +546,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 133, "metadata": {}, "outputs": [], "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", + "set_to_remove = set(list_to_remove)" + ] + }, + { + "cell_type": "code", + "execution_count": 140, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 4, 6, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 30, 32, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 70, 73, 74, 75, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99}\n" + ] + } + ], + "source": [ + "print(set1)" + ] + }, + { + "cell_type": "code", + "execution_count": 141, + "metadata": {}, + "outputs": [], + "source": [ + "set1.difference_update(set_to_remove)" + ] + }, + { + "cell_type": "code", + "execution_count": 142, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 4, 6, 8, 10, 13, 14, 15, 16, 17, 18, 20, 22, 23, 24, 25, 26, 27, 28, 30, 32, 34, 35, 36, 38, 40, 42, 43, 44, 45, 46, 47, 48, 50, 52, 53, 54, 55, 56, 58, 60, 62, 64, 65, 66, 67, 68, 70, 73, 74, 75, 78, 82, 83, 84, 85, 86, 87, 88, 90, 92, 93, 94, 95, 96, 98}\n" + ] + } + ], + "source": [ + "print(set1)" ] } ], @@ -307,7 +614,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.7.6" } }, "nbformat": 4, diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 74d65e0..8a248e7 100755 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -21,11 +21,11 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "dict1 = {'Penelope':178, 'Arnau':172, 'Marta':175}" ] }, { @@ -37,11 +37,62 @@ }, { "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" + "dict1.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([178, 172, 175])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict1.values()" + ] + }, + { + "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 +104,20 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Penelope': 178, 'Arnau': 172, 'Marta': 175, 'Pau': 215}\n" + ] + } + ], "source": [ - "# Your code here\n" + "dict1['Pau'] = 215\n", + "print(dict1)" ] }, { @@ -69,11 +129,11 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "del dict1['Pau']" ] }, { @@ -87,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -121,11 +181,49 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "keys = list(word_freq.keys())" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "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": [ + "keys.sort()\n", + "print(keys)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "word_freq2 = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "for i in keys:\n", + " word_freq[i]\n", + " " ] }, { @@ -186,7 +284,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.7.6" } }, "nbformat": 4, From 69073a2c48a22723b22da774f1d122079e7426e0 Mon Sep 17 00:00:00 2001 From: Jyu-as Date: Thu, 22 Oct 2020 22:29:55 +0200 Subject: [PATCH 2/2] lab-tuple-set-dict --- your-code/challenge-1.ipynb | 18 ++--- your-code/challenge-3.ipynb | 144 +++++++++++++++++++++++++++++++++--- 2 files changed, 144 insertions(+), 18 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 08a49ce..257be95 100755 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -42,7 +42,7 @@ "tuple" ] }, - "execution_count": 31, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -76,7 +76,7 @@ "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtup\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"o\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"n\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"h\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"a\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"c\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"k\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtup\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"o\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"n\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"h\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"a\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"c\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"k\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" ] } @@ -87,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -109,7 +109,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -132,7 +132,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -163,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 9, "metadata": {}, "outputs": [ { diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 8a248e7..a4bc4c2 100755 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -147,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -181,7 +181,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -190,7 +190,24 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['love', 'conversation', 'every', \"we're\", 'plate', 'sour', 'jukebox', 'now', 'taxi', 'fast', 'bag', 'man', 'push', 'baby', 'going', 'you', \"don't\", 'one', 'mind', 'backseat', 'friends', 'then', 'know', 'take', 'play', 'okay', 'so', 'begin', 'start', 'over', 'body', 'boy', 'just', 'we', 'are', 'girl', 'tell', 'singing', 'drinking', 'put', 'our', 'where', \"i'll\", 'all', \"isn't\", 'make', 'lover', 'get', 'radio', 'give', \"i'm\", 'like', 'can', 'doing', 'with', 'club', 'come', 'it', 'somebody', 'handmade', 'out', 'new', 'room', 'chance', 'follow', 'in', 'may', 'brand', 'that', 'magnet', 'up', 'first', 'and', 'pull', 'of', 'table', 'much', 'last', 'i', 'thrifty', 'grab', 'was', 'driver', 'slow', 'dance', 'the', 'say', 'trust', 'family', 'week', 'date', 'me', 'do', 'waist', 'smell', 'day', 'although', 'your', 'leave', 'want', \"let's\", 'lead', 'at', 'hand', 'how', 'talk', 'not', 'eat', 'falling', 'about', 'story', 'sweet', 'best', 'crazy', 'let', 'too', 'van', 'shots', 'go', 'to', 'a', 'my', 'is', 'place', 'find', 'shape', 'on', 'kiss', 'were', 'night', 'heart', 'for', 'discovering', 'something', 'be', 'bedsheets', 'fill', 'hours', 'stop', 'bar']\n" + ] + } + ], + "source": [ + "print(keys)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -208,7 +225,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ @@ -217,15 +234,65 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 35, "metadata": {}, "outputs": [], "source": [ + "values = []\n", "for i in keys:\n", - " word_freq[i]\n", - " " + " values.append(word_freq[i])" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[8, 1, 1, 3, 23, 1, 1, 14, 1, 1, 1, 16, 3, 1, 1, 17, 2, 6, 1, 1, 1, 37, 1, 2, 1, 1, 6, 6, 3, 2, 2, 1, 1, 1, 6, 3, 1, 1, 2, 1, 1, 6, 3, 1, 1, 2, 1, 2, 1, 2, 1, 2, 3, 2, 1, 6, 1, 23, 27, 5, 1, 1, 1, 1, 1, 2, 3, 6, 1, 1, 2, 10, 25, 1, 3, 1, 1, 2, 10, 2, 2, 33, 6, 3, 2, 11, 6, 1, 40, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 3, 2, 6, 1, 2, 1, 3, 2, 2, 6, 1, 2, 1, 1, 1, 1, 1, 4, 1, 1, 2, 18, 3, 1, 2, 5, 1, 3, 1, 2, 2, 2, 7, 1, 1, 3, 1, 22, 16, 21]\n" + ] + } + ], + "source": [ + "print(values)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "word_freq2 = dict(zip(keys, values))" ] }, + { + "cell_type": "code", + "execution_count": 39, + "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": [ + "print(word_freq2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -260,12 +327,71 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "import operator" + ] + }, + { + "cell_type": "code", + "execution_count": 42, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "sorted_tups = sorted(word_freq.items(), key = operator.itemgetter(1))" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('conversation', 1), (\"we're\", 1), ('plate', 1), ('sour', 1), ('jukebox', 1), ('taxi', 1), ('fast', 1), ('bag', 1), ('man', 1), ('going', 1), ('one', 1), ('backseat', 1), ('friends', 1), ('take', 1), ('play', 1), ('okay', 1), ('begin', 1), ('over', 1), ('just', 1), ('are', 1), ('tell', 1), ('drinking', 1), ('our', 1), ('where', 1), (\"i'll\", 1), ('all', 1), (\"isn't\", 1), ('make', 1), ('lover', 1), ('get', 1), ('radio', 1), ('give', 1), ('can', 1), ('club', 1), ('it', 1), ('out', 1), ('chance', 1), ('first', 1), ('table', 1), ('thrifty', 1), ('driver', 1), ('slow', 1), ('dance', 1), ('trust', 1), ('family', 1), ('week', 1), ('date', 1), ('leave', 1), ('at', 1), ('hand', 1), ('how', 1), ('eat', 1), ('about', 1), ('story', 1), ('sweet', 1), ('best', 1), ('let', 1), ('van', 1), ('shots', 1), ('place', 1), ('find', 1), ('kiss', 1), ('stop', 1), ('bar', 1), (\"don't\", 2), ('mind', 2), ('know', 2), ('so', 2), ('start', 2), ('boy', 2), ('girl', 2), ('singing', 2), ('doing', 2), ('somebody', 2), ('handmade', 2), ('may', 2), ('that', 2), ('much', 2), ('grab', 2), ('was', 2), ('say', 2), ('waist', 2), ('want', 2), (\"let's\", 2), ('not', 2), ('crazy', 2), ('go', 2), ('to', 2), ('fill', 2), ('hours', 2), ('push', 3), ('then', 3), ('put', 3), ('room', 3), ('magnet', 3), ('up', 3), ('pull', 3), ('last', 3), ('do', 3), ('smell', 3), ('although', 3), ('falling', 3), ('were', 3), ('night', 3), ('heart', 3), ('for', 3), ('bedsheets', 3), ('talk', 4), ('too', 5), ('is', 5), ('every', 6), ('new', 6), ('follow', 6), ('brand', 6), ('of', 6), ('i', 6), ('day', 6), ('lead', 6), ('shape', 6), ('discovering', 6), ('something', 6), ('we', 7), ('a', 8), ('like', 10), ('me', 10), ('now', 11), ('baby', 14), ('you', 16), ('be', 16), ('body', 17), ('the', 18), ('your', 21), ('with', 22), (\"i'm\", 23), ('and', 23), ('love', 25), ('in', 27), ('my', 33), ('come', 37), ('on', 40)]\n" + ] + } + ], + "source": [ + "print(sorted_tups)" ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [], + "source": [ + "word_freq2 = dict(sorted_tups)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'conversation': 1, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'going': 1, 'one': 1, 'backseat': 1, 'friends': 1, 'take': 1, 'play': 1, 'okay': 1, 'begin': 1, 'over': 1, 'just': 1, 'are': 1, 'tell': 1, 'drinking': 1, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, 'can': 1, 'club': 1, 'it': 1, 'out': 1, 'chance': 1, 'first': 1, 'table': 1, 'thrifty': 1, 'driver': 1, 'slow': 1, 'dance': 1, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'leave': 1, 'at': 1, 'hand': 1, 'how': 1, 'eat': 1, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'let': 1, 'van': 1, 'shots': 1, 'place': 1, 'find': 1, 'kiss': 1, 'stop': 1, 'bar': 1, \"don't\": 2, 'mind': 2, 'know': 2, 'so': 2, 'start': 2, 'boy': 2, 'girl': 2, 'singing': 2, 'doing': 2, 'somebody': 2, 'handmade': 2, 'may': 2, 'that': 2, 'much': 2, 'grab': 2, 'was': 2, 'say': 2, 'waist': 2, 'want': 2, \"let's\": 2, 'not': 2, 'crazy': 2, 'go': 2, 'to': 2, 'fill': 2, 'hours': 2, 'push': 3, 'then': 3, 'put': 3, 'room': 3, 'magnet': 3, 'up': 3, 'pull': 3, 'last': 3, 'do': 3, 'smell': 3, 'although': 3, 'falling': 3, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'bedsheets': 3, 'talk': 4, 'too': 5, 'is': 5, 'every': 6, 'new': 6, 'follow': 6, 'brand': 6, 'of': 6, 'i': 6, 'day': 6, 'lead': 6, 'shape': 6, 'discovering': 6, 'something': 6, 'we': 7, 'a': 8, 'like': 10, 'me': 10, 'now': 11, 'baby': 14, 'you': 16, 'be': 16, 'body': 17, 'the': 18, 'your': 21, 'with': 22, \"i'm\": 23, 'and': 23, 'love': 25, 'in': 27, 'my': 33, 'come': 37, 'on': 40}\n" + ] + } + ], + "source": [ + "print(word_freq2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {